I am an avid fan of the Spring framework for Java (by Rod Johnson). I am learning Python and was excited to find about Spring for Python. I would be interested in hearing the community's views on the comparison of these two flavours of Spring. How well does it fit Python's paradigms etc.
Dependency injection frameworks are not nearly as useful in a dynamically typed language. See for example the presentation Dependency Injection: Vitally important or totally irrelevant? In Java the flexibility provided by a dependency injection framework is vital, while in Python it usually results in unneeded complexity.
This doesn't mean that the principles are wrong. See this example how to achieve loose coupling between classes by using simple idioms:
# A concrete class implementing the greeting provider interface
class EnglishGreetingProvider(object):
def get_greeting(self, who):
return "Hello %s!" % who
# A class that takes a greeting provider factory as a parameter
class ConsoleGreeter(object):
def __init__(self, who, provider=EnglishGreetingProvider):
self.who = who
self.provider = provider()
def greet(self):
print(self.provider.get_greeting(self.who))
# Default wiring
greeter = ConsoleGreeter(who="World")
greeter.greet()
# Alternative implementation
class FrenchGreetingProvider(object):
def get_greeting(self, who):
return "Bonjour %s!" % who
greeter = ConsoleGreeter(who="World", provider=FrenchGreetingProvider)
greeter.greet()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With