Skip to content

Instantly share code, notes, and snippets.

@vortec
Created March 27, 2015 22:18
Show Gist options
  • Save vortec/18ff373155c6141515bb to your computer and use it in GitHub Desktop.
Save vortec/18ff373155c6141515bb to your computer and use it in GitHub Desktop.
class Dependency:
def get_cheese_from_singleton(self):
from foobar_controller import FoobarController
liked = FoobarController.foo_the_bar()
return 'I like {}'.format(liked)
def get_cheese_from_di(self, foobar_controller):
liked = foobar_controller.foo_the_bar()
return 'I like {}'.format(liked)
class FoobarController:
foobar = None
@classmethod
def foo_the_bar(cls):
return cls.foobar
if __name__ == '__main__':
import sys
from foobar_controller import FoobarController
from dependency import Dependency
# Get values for service class from somewhere and apply them
FoobarController.foobar = sys.argv[1]
dep = Dependency()
# Let the dependency figure out how to get the service class.
# For testing you could mock it.
print(dep.get_cheese_from_singleton())
# DI
print(dep.get_cheese_from_di(FoobarController))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment