Skip to content

Instantly share code, notes, and snippets.

@nemec

nemec/di.py Secret

Created June 2, 2013 15:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nemec/5e3735bff23d177e6120 to your computer and use it in GitHub Desktop.
Save nemec/5e3735bff23d177e6120 to your computer and use it in GitHub Desktop.
Poor man's Dependency Injection
user = None
import di
def now():
if not di.user:
print "Unauthenticated user"
else:
print "Authenticated as {0}".format(di.user)
import di
import library
library.now()
di.user = "Fred"
library.now()
# $ python main.py
# Unauthenticated user
# Authenticated as Fred
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment