by nfrbc · 7y ago · 0 comments
Dependency injection can be as simple as:
class Printer:
def run(self):
print("hello world")
class Caller:
def __init__(self, printer_class=Printer):
self.printer = printer_class()
def doit(self):
self.printer.run()
In your tests all you have to do is pass a different object to your Caller class. No need to mess with factories, interfaces or Impl suffixes.If the point is just having simpler testability and making dependencies more explicit, this gets the job done.
And since Python is a dynamic language, you can use this with stdlib classes too, so it solves your first complaint.
I've seen it a lot in Ruby and C# lately.
No comments yet.