Skip to content

Instantly share code, notes, and snippets.

@hbradio
Created August 23, 2017 14:37
Show Gist options
  • Save hbradio/3e99744fec61c1cbde2902f429d9ecbd to your computer and use it in GitHub Desktop.
Save hbradio/3e99744fec61c1cbde2902f429d9ecbd to your computer and use it in GitHub Desktop.
import lib
# A not-great example
class MyLessGoodClass:
def do_something(self):
x = lib.get_thing()
return x + 1
def do_another_thing(self):
x = lib.get_thing()
return x + 2
a_less_good_instance = MyLessGoodClass()
a_less_good_instance.do_something()
_less_good_instance.do_another_thing()
# An example showing better dependency injection
class MyBetterClass:
__init__(self, thing_provider):
self.thing_provider = thing_provider
def do_something(self):
x = self.thing_provider.get_thing()
return x + 1
def do_another_thing(self):
x = self.thing_provider.get_thing()
return x + 2
a_better_instance = MyBetterClass(thing_provider=lib)
a_better_instance.do_something()
a_better_instance.do_another_thing()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment