1

This question is not focused on the DAO, MVC, Messaging and other component frameworks that Spring is known for, but just the Core.

As I understand it, Spring's earlier advantage was that it managed the component lifecycle, providing a Singleton Factory that creates the objects in the configuration file read on startup.

The singleton factory then creates all the service beans upon request as singletons. This is one of the big wins with Spring, as it uses less memory and there's a lot less garbage collection in the JVM. Data objects on the other hand are still created and destroyed, often not as singletons.

Why doesn't dependency injection have such big advantages for python or other languages? Or are they being used as such?

shigeta
  • 1,791
  • 3
  • 19
  • 32
  • 1
    Go see if this respond what you're asking http://stackoverflow.com/questions/2461702/why-is-ioc-di-not-common-in-python – jordenysp Sep 19 '11 at 05:16
  • there's spring python http://springpython.webfactional.com/ it's just the idea of DI as it is in java land is not all that popular(whether that's right or wrong). zope component architecture kind of meets some of the same needs as DI and then some. compared to spring python it is more popular but then there are some people who HATE zca as well. – Tom Willis Sep 19 '11 at 13:48

2 Answers2

1

Python has a different relative cost of operations. Instead of garbage collection, it uses reference counting, which means that releasing objects is not as expensive as in Java. Using singletons just for the speedup is not very good design in my opinion, as it makes more interdependencies between different parts of the system.

On the other hand, the design benefits of dependency injection (clearer code, better encapsulation) apply also to Python, and there exists such frameworks: Python Dependency Injection Framework

Community
  • 1
  • 1
jpa
  • 10,351
  • 1
  • 28
  • 45
0

Well, I have never felt the need to use such a thing in Python - and most Python programmers I know did not looked for something like this either. I have the impression that Python objects are lightweight in general and it would be better to create a lot of them and avoid the inherent problems of mutable objects than to use singletons for the sake of optimization.

Anyway, if one really wants some kind of singleton, the usual way is to create a value in a module:

# Module stuff.py
class Stuff(object):
     # ....
     pass

singleton = new Stuff()

Then you use it:

import stuff
stuff.singleton.do_something()

You can even, let us say, replace the value in the module, as you would do by swapping the applicationContext.xml files of Spring. It can be useful in tests, for example:

import stuff
stuff.singleton = MockedStuff()

class MyTestCase(TestCase):
    def testMockStuff(self):
        # Component can be a class which uses the singleton
        component = Component()
        # Proceed with tests

Actually, you can mock even the whole class:

import stuff
stuff.Stuff = MockedStuff

class MyTestCase(TestCase):
    def testMockStuff(self):
        # Component creates some instance of stuff.Stuff, which is mocked now
        component = Component()
        # Proceed with tests

Summarizing: IMHO, Python programmers do not feel the necessity of something like Spring DI. If one needs some functionality of Spring DI, it is easy to implement through pure Python code.

(There are points based in my experience. I am sure more people can point another causes.)

brandizzi
  • 26,083
  • 8
  • 103
  • 158