Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Spring for Python compare with Spring for Java

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.

like image 980
Sathya Avatar asked Jun 02 '09 16:06

Sathya


1 Answers

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()
like image 148
Ants Aasma Avatar answered Sep 20 '22 18:09

Ants Aasma