425

In Java IoC / DI is a very common practice which is extensively used in web applications, nearly all available frameworks and Java EE. On the other hand, there are also lots of big Python web applications, but beside of Zope (which I've heard should be really horrible to code) IoC doesn't seem to be very common in the Python world. (Please name some examples if you think that I'm wrong).

There are of course several clones of popular Java IoC frameworks available for Python, springpython for example. But none of them seems to get used practically. At least, I've never stumpled upon a Django or sqlalchemy+<insert your favorite wsgi toolkit here> based web application which uses something like that.

In my opinion IoC has reasonable advantages and would make it easy to replace the django-default-user-model for example, but extensive usage of interface classes and IoC in Python looks a bit odd and not »pythonic«. But maybe someone has a better explanation, why IoC isn't widely used in Python.

9
  • 3
    My guess, same reason that it is less popular in Ruby, built-in mixins and open classes Mar 17, 2010 at 11:20
  • 3
    you ever tried springpython? it doesn't even work as advertised. at least in the aop portion. everything else in there is not very useful unless you are coming from java and need some level of comfort during the transition.
    – Tom Willis
    Mar 17, 2010 at 17:47
  • 15
    Please take care to distinguish between the use of DI, and the use of an IOC framework. The former is a design pattern, the latter is a framework to assist in the automated use of the former.
    – Doug
    Nov 20, 2011 at 14:30
  • 11
    I'd love to see an answer that addresses the real world problems that DI solves: Lifetime management, ease of test stubbing, etc. If there's a more Pythonic way to tackle these, I'm all ears.
    – Josh Noe
    Jun 29, 2018 at 0:35
  • 3
    @njappboy What? Dec 13, 2018 at 21:56

18 Answers 18

222

I don't actually think that DI/IoC are that uncommon in Python. What is uncommon, however, are DI/IoC frameworks/containers.

Think about it: what does a DI container do? It allows you to

  1. wire together independent components into a complete application ...
  2. ... at runtime.

We have names for "wiring together" and "at runtime":

  1. scripting
  2. dynamic

So, a DI container is nothing but an interpreter for a dynamic scripting language. Actually, let me rephrase that: a typical Java/.NET DI container is nothing but a crappy interpreter for a really bad dynamic scripting language with butt-ugly, sometimes XML-based, syntax.

When you program in Python, why would you want to use an ugly, bad scripting language when you have a beautiful, brilliant scripting language at your disposal? Actually, that's a more general question: when you program in pretty much any language, why would you want to use an ugly, bad scripting language when you have Jython and IronPython at your disposal?

So, to recap: the practice of DI/IoC is just as important in Python as it is in Java, for exactly the same reasons. The implementation of DI/IoC however, is built into the language and often so lightweight that it completely vanishes.

(Here's a brief aside for an analogy: in assembly, a subroutine call is a pretty major deal - you have to save your local variables and registers to memory, save your return address somewhere, change the instruction pointer to the subroutine you are calling, arrange for it to somehow jump back into your subroutine when it is finished, put the arguments somewhere where the callee can find them, and so on. IOW: in assembly, "subroutine call" is a Design Pattern, and before there were languages like Fortran which had subroutine calls built in, people were building their own "subroutine frameworks". Would you say that subroutine calls are "uncommon" in Python, just because you don't use subroutine frameworks?)

BTW: for an example of what it looks like to take DI to its logical conclusion, take a look at Gilad Bracha's Newspeak Programming Language and his writings on the subject:

32
  • 78
    While I agree. The XML comment is wrong. Many (at least the modern) IOC containers use convention (code) over configuration (XML).
    – Finglas
    May 19, 2010 at 9:32
  • 192
    "The implementation of DI/IoC however, is built into the language and often so lightweight that it completely vanishes." Down vote because this is categorically untrue. DI is a pattern where an interface is passed in to the constructor. It is not built-in in python.
    – Doug
    Nov 20, 2011 at 14:33
  • 198
    downvote, wiring together has nothing to do with scripting, DI is a pattern, and it is not equivalent to scripting
    – Luxspes
    Dec 23, 2012 at 6:06
  • 55
    I disagree with this. DI doesn't solve the lack of dynamic scripting in static languages. It provides a framework for configuring and composing your application's parts. I once heard a Ruby dev say that DI is unnecessary in dynamic languages. But he used Rails... Rails is just a big DI container of sorts, which uses convention to figure out which parts to configure when. He didn't need DI because Rails solved the problem of finding the parts for him. Sep 25, 2013 at 9:56
  • 27
    Downvoted. Too much babbling about how wonderful the language is and yet the response about the aforementioned pattern is fundamentally wrong.
    – marcio
    Nov 21, 2017 at 3:28
91

IoC and DI are super common in mature Python code. You just don't need a framework to implement DI thanks to duck typing.

The best example is how you set up a Django application using settings.py:

# settings.py
CACHES = {
    'default': {
        'BACKEND': 'django_redis.cache.RedisCache',
        'LOCATION': REDIS_URL + '/1',
    },
    'local': {
        'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
        'LOCATION': 'snowflake',
    }
}

Django Rest Framework utilizes DI heavily:

class FooView(APIView):
    # The "injected" dependencies:
    permission_classes = (IsAuthenticated, )
    throttle_classes = (ScopedRateThrottle, )
    parser_classes = (parsers.FormParser, parsers.JSONParser, parsers.MultiPartParser)
    renderer_classes = (renderers.JSONRenderer,)

    def get(self, request, *args, **kwargs):
        pass

    def post(self, request, *args, **kwargs):
        pass

Let me remind (source):

"Dependency Injection" is a 25-dollar term for a 5-cent concept. [...] Dependency injection means giving an object its instance variables. [...].

15
  • 12
    +1. Well put. Being a Python programmer I was completely baffled by a whole interview presentation on DI frameworks in C#. Took me a while to realise I already did it all the time in Flask apps without even thinking about it because you don't need a framework. To someone who doesn't know anything beyond C#/Java the question makes sense. To duck-typed language programmers it is just natural and as you say, "25-dollar term for a 5-cent concept". Apr 4, 2019 at 8:13
  • 27
    err... this is not dependency injection as the instances (IsAuthenticated, ScopedRateThrottle) are instantiated by the class. They are not passed into the constructor.
    – dopatraman
    May 7, 2019 at 19:36
  • 15
    IsAuthenticated and ScopedRateThrottle are not instances, these are classes. They are instantiated when a FooView is constructed (actually, when the FooView processes a request). Anyway, this is merely an implementation detail. IsAuthenticated and ScopedRateThrottle are the dependencies; they are injected into the FooView. It doesn't matter when or how this is done. Python is not Java, so there are different ways to implement this.
    – Max Malysh
    May 7, 2019 at 20:36
  • 19
    @MaxMalysh I agree with dopatraman on this one. This is not even IoC as the class itself has "hardcoded" dependencies to a specific class. In IoC, the dependency shoudl be provided instead of hardcoded. In top of that, in Dependency Injection, you will have an entity responsible of managing the lifecycles of each service and inject them when that is the case. The solution provided in not any of those. Nov 26, 2019 at 20:15
  • 9
    @alex No, you don't need to change your code to use another renderer. You can even use multiple renderers simultaneously: renderer_classes = (JSONRenderer, BrowsableAPIRenderer, XMLRenderer). Mocking is as simple as @unittest.patch('myapp.views.FooView.permission_classes'). A desperate need to "pass something" is a consequence of the "Java way of doing things" due to Java being a compiled and statically typed language lacking strong metaprogramming capabilities.
    – Max Malysh
    Apr 14, 2020 at 19:13
69

Part of it is the way the module system works in Python. You can get a sort of "singleton" for free, just by importing it from a module. Define an actual instance of an object in a module, and then any client code can import it and actually get a working, fully constructed / populated object.

This is in contrast to Java, where you don't import actual instances of objects. This means you are always having to instantiate them yourself, (or use some sort of IoC/DI style approach). You can mitigate the hassle of having to instantiate everything yourself by having static factory methods (or actual factory classes), but then you still incur the resource overhead of actually creating new ones each time.

7
  • 2
    That makes sense. If I want to change an implementation in Python, I simply import from a different location using the same name. But now I am thinking if it's also possible the other way round by defining a MyClassInstances class to each MyClass in Java, which contains only static, fully initialized instances. That would be wired :D
    – tux21b
    Mar 17, 2010 at 13:45
  • 3
    And another idea: Providing a way of changing such imports in python would it make it possible to replace implementations easily without touching all the python files. Instead of from framework.auth.user import User it might be better to write User = lookup('UserImplentation', 'framework.auth.user.User') (the 2nd parameter might be a default value) inside the framework. Then users of the framework would be able to replace/specialize the User implementation without touching the framework.
    – tux21b
    Mar 17, 2010 at 13:56
  • 26
    Oversimplifying, answer, in real life, you rarely need just "a singleton", you need to control scope (you might need a thread local singleton, or a session singleton, and so on), this makes me think that the kind of problems solved in Python are not the kind of real world problems actually solved in an enterprise setting
    – Luxspes
    Dec 23, 2012 at 6:09
  • 5
    Actually DI is about being able to test and decouple dependencies of code. Also the import feature is similar to static imports in Java, which let me import a single instance of an object. Nov 28, 2014 at 12:47
  • 3
    "You can get a sort of "singleton" for free, just by importing it from a module."can be easily done in Java by declaring a static instance field and setting it to a value. This isn't a sol
    – ggranum
    Jan 2, 2018 at 16:40
37

Django makes great use of inversion of control. For instance, the database server is selected by the configuration file, then the framework provides appropriate database wrapper instances to database clients.

The difference is that Python has first-class types. Data types, including classes, are themselves objects. If you want something to use a particular class, simply name the class. For example:

if config_dbms_name == 'postgresql':
    import psycopg
    self.database_interface = psycopg
elif config_dbms_name == 'mysql':
    ...

Later code can then create a database interface by writing:

my_db_connection = self.database_interface()
# Do stuff with database.

Instead of the boilerplate factory functions that Java and C++ need, Python does it with one or two lines of ordinary code. This is the strength of functional versus imperative programming.

4
  • 5
    What you call code is actually the wiring part. That would be the XML of your ioc framework. It could actually be written simply as import psycopg2 as database_interface. Put that line in a injections.py et voilà.
    – spectras
    Oct 23, 2015 at 19:56
  • 43
    Erm. What your doing there is pretty much textbook imperative Daniel.
    – Shayne
    Nov 9, 2015 at 16:23
  • 1
    It's definitely imperative code, but it's kind of functional because it uses a callable as a value.
    – Jeremy
    May 19, 2016 at 23:41
  • 6
    Isn't that just First Class Functions though? en.wikipedia.org/wiki/First-class_function Just because you have and use them doesn't make your code Functional. There are quite a few side effects happening here (such as changing self.database_interface), which screams imperative.
    – hjc1710
    Jul 21, 2016 at 19:16
34

It seems that people really don't get what Dependency injection and inversion of control mean anymore.

The practice of using inversion of control is to have classes or functions that depend on other classes or functions, but instead of creating the instances whithin the class or function code it is better to receive them as parameters, so loose coupling can be achieved. That has many benefits as more testability and to achieve the liskov substitution principle.

You see, by working with interfaces and injections, your code gets more maintainable, since you can change the behavior easily, because you won't have to rewrite a single line of code (maybe a line or two on the DI configuration) of your class to change its behavior, since the classes that implement the interface your class is waiting for can vary independently as long as they follow the interface. One of the best strategies to keep code decoupled and easy to maintain is to follow at least the single responsibility, substitution and dependency inversion principles.

What's a DI library good for if you can instantiate an object yourself inside a package and import it to inject it yourself? The chosen answer is right, since java has no procedural sections (code outside of classes), all that goes into boring configuration xml's, hence the need of a class to instantiate and inject dependencies on a lazy load fashion so you don't blow away your performance, while on python you just code the injections in the "procedural" (code outside classes) sections of your code.

3
  • 4
    you still miss that an IoC/DI wire the objects together automatically. It's not much being able to do it at runtime (Java can do that via reflection anyway), it's that the framework takes care of it and you don't need to do it explicitly. Having procedural sections is irrelevant either, nothing prevents one to write an entirely procedural app in Java, by using classes as mere containers of static subroutines and functions, without using OOP features at all.
    – zakmck
    Jan 11, 2020 at 10:04
  • 3
    @zakmck: the "procedural" section of Python here isn't really about writing procedural code. What makes the "procedural" section of Python different than static languages is the ability to put procedural code in a class body, which runs during class definition time, and put import statements inside if-statement, and to create class factory simply by defining classes inside a factory method. These are things that you can't really do in static languages, and which solves most of the problems that IOC/DI tried to solve. Metaprogramming in Python often looks just like regular Python code.
    – Lie Ryan
    Jan 20, 2020 at 7:22
  • 3
    @LieRyan, you can do that with reflection, or, if you need it often or at runtime, you can call the static language from another language like Groovy (which is designed to easily play with Java), or even Python itself. Yet, that has little to do with IoC/DI frameworks, for their purpose is to do most of the procedural object wiring for you, automatically, leveraging on definitions only. Sadly, most of the hereby answers miss this point.
    – zakmck
    Jan 20, 2020 at 16:19
14

Haven't used Python in several years, but I would say that it has more to do with it being a dynamically typed language than anything else. For a simple example, in Java, if I wanted to test that something wrote to standard out appropriately I could use DI and pass in any PrintStream to capture the text being written and verify it. When I'm working in Ruby, however, I can dynamically replace the 'puts' method on STDOUT to do the verify, leaving DI completely out of the picture. If the only reason I'm creating an abstraction is to test the class that's using it (think File system operations or the clock in Java) then DI/IoC creates unnecessary complexity in the solution.

4
  • 6
    It never ceases to amaze me that people willing to change how a system works to test that it worked. Now you need to test that your tests don't cause side-effects.
    – Basic
    Jul 26, 2016 at 14:49
  • 3
    he talks about changing puts method only in tests scope, it is like mock method of injected object.
    – dpa
    Nov 24, 2016 at 17:58
  • 3
    @Basic that's pretty normal in unit tests, actually it is advisable to do that in these tests as you don't want to pollute your test case coverage with more than one block of code (the one that is being tested). It would be wrong to do that for integration tests though, maybe that is what you are referring to on your comment? Jan 14, 2018 at 20:11
  • 4
    For me, testability is a first class concern. If a design isn't testable, it's not a good design, and I have no problem changing the design in order to make it more testable. I'll have to revalidate that it still works, but that's ok. Testability is a perfectly valid reason to change code IMO Jan 13, 2020 at 19:55
12

Actually, it is quite easy to write sufficiently clean and compact code with DI (I wonder, will it be/stay pythonic then, but anyway :) ), for example I actually perefer this way of coding:

def polite(name_str):
    return "dear " + name_str

def rude(name_str):
    return name_str + ", you, moron"

def greet(name_str, call=polite):
    print "Hello, " + call(name_str) + "!"

_

>>greet("Peter")
Hello, dear Peter!
>>greet("Jack", rude)
Hello, Jack, you, moron!

Yes, this can be viewed as just a simple form of parameterizing functions/classes, but it does its work. So, maybe Python's default-included batteries are enough here too.

P.S. I have also posted a larger example of this naive approach at Dynamically evaluating simple boolean logic in Python.

2
  • 4
    For simple cases that might work, but just imagine a simple web blog controller, which uses various models (Post, Comment, User). If you want the user to inject his own Post model (with an additional viewcount attribute to track that), and his own User model with more profile information and so on, all the parameters might look confusing. Additionally, the user might want to change the Request object too, to support filesystem session instead of simple cookie based session or something like that... So, you will end up with lots of parameters shortly.
    – tux21b
    Mar 18, 2010 at 21:25
  • 1
    @tux21b Well, there's "essential complexity" the users want the application to implement, there are architectural solutions to it (some of which are not worse than the rest of them in terms of development and possibly maintenance time, exec. speed, etc.), and there's human ability to comprehend the API and software architecture. If there's no human-comprehensible solution at all (not just among those using (any form of) DI)... well, who said that all problems are solvable? And having lots of default-assigned (but swappable by user's choice) parameters may actually suffice often.
    – mlvljr
    Mar 18, 2010 at 21:45
10

IoC/DI is a design concept, but unfortunately it's often taken as a concept that applies to certain languages (or typing systems). I'd love to see dependency injection containers become far more popular in Python. There's Spring, but that's a super-framework and seems to be a direct port of the Java concepts without much consideration for "The Python Way."

Given Annotations in Python 3, I decided to have a crack at a full featured, but simple, dependency injection container: https://github.com/zsims/dic . It's based on some concepts from a .NET dependency injection container (which IMO is fantastic if you're ever playing in that space), but mutated with Python concepts.

8

I think due to the dynamic nature of python people don't often see the need for another dynamic framework. When a class inherits from the new-style 'object' you can create a new variable dynamically (https://wiki.python.org/moin/NewClassVsClassicClass).

i.e. In plain python:

#application.py
class Application(object):
    def __init__(self):
        pass

#main.py
Application.postgres_connection = PostgresConnection()

#other.py
postgres_connection = Application.postgres_connection
db_data = postgres_connection.fetchone()

However have a look at https://github.com/noodleflake/pyioc this might be what you are looking for.

i.e. In pyioc

from libs.service_locator import ServiceLocator

#main.py
ServiceLocator.register(PostgresConnection)

#other.py
postgres_connection = ServiceLocator.resolve(PostgresConnection)
db_data = postgres_connection.fetchone()
3
  • 2
    The very fact both version take the same amount of code goes a long way towards explaining why using a framework is not very popular.
    – spectras
    Oct 23, 2015 at 20:22
  • In other.py line 1, there is an automated dependency resolution, but wouldn't count that as a dependency injection though.
    – andho
    Mar 20, 2018 at 8:06
  • 1
    Service locators are usually an anti-pattern, just saying.
    – PmanAce
    Feb 21, 2020 at 19:01
8

pytest fixtures all based on DI (source)

7

Check out FastAPI, it has dependency injection built-in. For example:

from fastapi import Depends, FastAPI

async def get_db():
    db = DBSession()
    try:
        yield db
    except Exception:
        db.rollback()
        raise
    finally:
        db.close()

app = FastAPI()

@app.get("/items")
def get_items(db=Depends(get_db)):
    return db.get_items()
5

I back "Jörg W Mittag" answer: "The Python implementation of DI/IoC is so lightweight that it completely vanishes".

To back up this statement, take a look at the famous Martin Fowler's example ported from Java to Python: Python:Design_Patterns:Inversion_of_Control

As you can see from the above link, a "Container" in Python can be written in 8 lines of code:

class Container:
    def __init__(self, system_data):
        for component_name, component_class, component_args in system_data:
            if type(component_class) == types.ClassType:
                args = [self.__dict__[arg] for arg in component_args]
                self.__dict__[component_name] = component_class(*args)
            else:
                self.__dict__[component_name] = component_class
2
  • 55
    This falls far short of even the weakest DI containers. Where's the lifetime management, recursive dependency resolution, ability to mock, or - failing all that - configuration? This is nothing more than a type lookup and cache which is not the same thing as IoC.
    – Basic
    Jul 26, 2016 at 14:47
  • 2
    Years ago I wrote a small DI framework using metaclasses as an exercise. The whole thing is a single file with zero imports and doctests that make it self-explainatory. It shows that basic features are not that hard to implement in a way that is even "pythonic", but I sincerely think it's sad that no complete solution has gotten major traction like Spring has in Java and everybody is doing custom plugin architectures. Jun 7, 2018 at 10:38
3

My 2cents is that in most Python applications you don't need it and, even if you needed it, chances are that many Java haters (and incompetent fiddlers who believe to be developers) consider it as something bad, just because it's popular in Java.

An IoC system is actually useful when you have complex networks of objects, where each object may be a dependency for several others and, in turn, be itself a dependant on other objects. In such a case you'll want to define all these objects once and have a mechanism to put them together automatically, based on as many implicit rules as possible. If you also have configuration to be defined in a simple way by the application user/administrator, that's an additional reason to desire an IoC system that can read its components from something like a simple XML file (which would be the configuration).

The typical Python application is much simpler, just a bunch of scripts, without such a complex architecture. Personally I'm aware of what an IoC actually is (contrary to those who wrote certain answers here) and I've never felt the need for it in my limited Python experience (also I don't use Spring everywhere, not when the advantages it gives don't justify its development overhead).

That said, there are Python situations where the IoC approach is actually useful and, in fact, I read here that Django uses it.

The same reasoning above could be applied to Aspect Oriented Programming in the Java world, with the difference that the number of cases where AOP is really worthwhile is even more limited.

19
  • Is there a reference URL to source of information where django uses IoC?
    – Sajuuk
    Nov 20, 2018 at 6:46
  • @Sajuuk, I've learned that about Django on this question's thread, so I don't know, you should ask the other answer authors.
    – zakmck
    Nov 20, 2018 at 9:35
  • The first alinea of this answer adds 0 value in my opinion... I think I am capable of deciding when my python code would benefit from IoC, and I do not care about what developer thinks what is bad. I value pragmatism over unfundamented opinions. Mar 1, 2019 at 16:53
  • 1
    @MikedeKlerk my suggestion is that something that is both unknown (as many answers hereby prove) and victim of prejudge is unlikely to become popular, no matter how objective and well informed a few like you are. And of course I'm not sure this is a reason why you don't see many IoC usage in Python, I think the main reason is low/medium compexity apps don't need them.
    – zakmck
    Mar 4, 2019 at 15:15
  • 1
    The typical Python application is much simpler, just a bunch of scripts, without such a complex architecture. - quite an assumption
    – hyankov
    Jan 6, 2020 at 17:11
3

You can do dependency injection with Python manually, but manual approach has its downsides:

  • lots of boilerplate code to do the wiring. You can use dynamic features of Python to do the injection, but then you're loosing IDE support (e.g. Ctrl+Space in PyCharm), and you're making code harder to understand and debug
  • no standards: every programmer has its own way for solving same problems, this leads to reinventing the wheel, understanding each other's code can quickly become a pain. Dependency injection library provides easy framework to plug-in

To have it all we NEED a dependency injection framework, for example this one https://python-dependency-injector.ets-labs.org/index.html seems to be the most mature DI framework for Python.

For smaller apps DI container is not necessary, for anything that has few hundred lines of code or more, DI container is a must have to keep your code maintaineable.

-1

I agree with @Jorg in the point that DI/IoC is possible, easier and even more beautiful in Python. What's missing is the frameworks supporting it, but there are a few exceptions. To point a couple of examples that come to my mind:

  • Django comments let you wire your own Comment class with your custom logic and forms. [More Info]

  • Django let you use a custom Profile object to attach to your User model. This is not completely IoC but is a good approach. Personally I'd like to replace the hole User model as the comments framework does. [More Info]

-2

IoC containers are "mimicked" mostly using **kwargs

class A:
    def __init__(self, **kwargs):
        print(kwargs)

Class B:
    pass

Class C:
    pass

Ainstance = A(b=B, c=C)
2
  • 2
    But what if B requires dependencies I, J, K, all of which also require one or more dependencies? Then you'd end up with Ainstance = A(b=B(i=I(...), j=J(...), k=K(...)). In contrast, you could have a framework look at type hints and/or some other form of config and instantiate all of those for you, and provide a fully instantiated A object for you. Aug 12, 2021 at 3:31
  • You are right! My bad. I did not think it that far. Aug 12, 2021 at 8:22
-4

In my opinion, things like dependency injection are symptoms of a rigid and over-complex framework. When the main body of code becomes much too weighty to change easily, you find yourself having to pick small parts of it, define interfaces for them, and then allowing people to change behaviour via the objects that plug into those interfaces. That's all well and good, but it's better to avoid that sort of complexity in the first place.

It's also the symptom of a statically-typed language. When the only tool you have to express abstraction is inheritance, then that's pretty much what you use everywhere. Having said that, C++ is pretty similar but never picked up the fascination with Builders and Interfaces everywhere that Java developers did. It is easy to get over-exuberant with the dream of being flexible and extensible at the cost of writing far too much generic code with little real benefit. I think it's a cultural thing.

Typically I think Python people are used to picking the right tool for the job, which is a coherent and simple whole, rather than the One True Tool (With A Thousand Possible Plugins) that can do anything but offers a bewildering array of possible configuration permutations. There are still interchangeable parts where necessary, but with no need for the big formalism of defining fixed interfaces, due to the flexibility of duck-typing and the relative simplicity of the language.

11
  • 4
    It isn't so much the framework as the language itself. To create the kind of flexibility that duck-typing languages enjoy, statically typed languages need very sophisticated frameworks and rules. DI is one of those rules. Python folks don't think twice about. Java folks have to really work at it.
    – S.Lott
    Mar 17, 2010 at 14:08
  • 6
    @S.Lott - I'd totally agree with you, except that C++ people seem to get by without the explosion of design and architecture patterns, despite working with similar restrictions to those of Java. I think that implies a cultural difference where, upon being faced with 2 possible ways to do something, Java people prefer to extract another interface to facilitate the Strategy pattern whereas C++ people dip right in and add a bool and an if statement...
    – Kylotan
    Mar 17, 2010 at 15:17
  • 4
    @Finglas so if I have a dozen classes all using my EmailSender and decide to replace it with a DesktopNotifier, I have to go and edit 12 classes by hand. And you think that's simpler and cleaner that just writing to an INotifier interface and letting the container work out the details?
    – Basic
    Jul 26, 2016 at 14:51
  • 2
    Unfortunately, a certain level of complexity is a reality that professional software developers must face. I see criticisms but no solutions in this answer. What is the "pythonic" solution for this problem: I am writing a library and I want to provide a hook for logging (something like PHP's PSR-3 LoggerInterface). I know how to use the log levels, but I don't care how the program actually reports them. What is the clean way to allow the client app to inject that implementation detail. Note: other parts of the application may have different implementations of this interface.
    – Rob
    Jan 8, 2017 at 14:55
  • 2
    My question for you is not how do you use the standard logging library, nor is it about creating different instances of a logger class. My question is how do you configure your application so that different parts of your application can use different implementations, and not be concerned with those details (provided that they know how to use the interface). This is a very real problem that DI has solved for multiple PHP applications I've worked on. I'm looking for the python equivalent. And suggesting "just don't make your application that complex" is not the answer I'm looking for.
    – Rob
    Jan 10, 2017 at 19:18
-7

Unlike the strong typed nature in Java. Python's duck typing behavior makes it so easy to pass objects around.

Java developers are focusing on the constructing the class strcuture and relation between objects, while keeping things flexible. IoC is extremely important for achieving this.

Python developers are focusing on getting the work done. They just wire up classes when they need it. They don't even have to worry about the type of the class. As long as it can quack, it's a duck! This nature leaves no room for IoC.

2
  • 6
    You still need to find a thing that quacks.
    – andho
    Mar 20, 2018 at 8:07
  • That's quite an opinionated answer. The language doesn't matter when getting things done.
    – thoroc
    Aug 28, 2020 at 14:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.