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()
Answer from Ants Aasma on Stack OverflowVideos
I've been working on something exciting - PySpring, a Python web framework that brings Spring Boot's elegance to Python. If you're tired of writing boilerplate code and want a more structured approach to web development, this might interest you!
- What's cool about it:
-
Auto dependency injection (no more manual wiring!)
-
Auto configuration management
-
Built on FastAPI for high performance
-
Component-based architecture
-
Familiar Spring Boot-like patterns
-
Recent PRs:
-
Route Mapping Decorators Implementation #3
-
Add Support for Qualifiers and Component Registration Validation
-
-
-
GitHub: https://github.com/PythonSpring/pyspring-core
-
Example Project: https://github.com/NFUChen/PySpring-Example-Project
Note: This project is in active development. I'm working on new features and improvements regularly. Your feedback and contributions would be incredibly valuable at this stage!If you like the idea of bringing Spring Boot's elegant patterns to Python or believe in making web development more structured and maintainable, I'd really appreciate if you could:
-
Star the repository
-
Share this with your network
-
Give it a try in your next project
Every star and share helps this project grow and reach more developers who might benefit from it. Thanks for your support! 🙏I'm actively maintaining this and would love your feedback! Feel free to star, open issues, or contribute. Let me know what you think!
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()
DISCLOSURE: I am the project lead for Spring Python, so you can consider my opinion biased.
I find that several of the options provided by Spring Python are useful including: aspect oriented programming, dependency injection, remoting, security, and easy database access.
Aspect oriented programming is, as they say, easier to implement off the cuff with python than java. But Spring Python makes it easy enough to add to existing python modules without editing their source code. The other solutions require meta-programming or modifying the original source code. I've already had one person visit our forums asking how to add an interceptor to a PyGame application, so he could unobtrusively "tap" some code.
Many people quickly assume "dependency injection" or "IoC" instantly means "XML configuration files". Not the case. While we support an XML configuration, just leap directly into using python decorators.
I already know about one company that is using Spring Python as a key piece of their system. They are interested in making improvements, adding new features, and generally using it as a piece of their solution. They have also experimented with running it inside jython, in case that piques your interest.
At the end of the day, my suggestion is to examine all the features, and see if any of them suit your needs. Whether this is adding needless complexity or succinct value can only be determined by you. You don't have to use everything; only what you need. To get some more info on what is available, I invite you to view Introduction to Spring Python, that I presented at SpringOne Americas 2008 conference.
» pip install pspring
Hi! I'm a webdev newbie and I'm currently looking for a good framework to try out. I'd like to know, regardless of my expertise and learning curve of either language, which would be a better choice for building a fast and reliable website? Java(SpringBoot) or Python(Flask/Django)? And can you give some examples of websites made with either language? If anybody has any other suggestions(like JS) besides these two languages/frameworks, feel free to tell me as well. Thanks in advance!! :D