1. Actually, I don't think direct investment/IoC is in Python. What is direct investment/IoC?
think about it: what is a DI container? It allows you to
connect into applications ...
... In
we have "wiring" and "
scripts
dynamic
So, a DI container is just an interpreted dynamic scripting language. In fact, another way is that a typical Java/.NET DI container is just a lame interpreter, a very bad dynamic scripting language and an ugly, often XML-based syntax.
When you are working in Python, why do you want an ugly and bad scripting language when you have a beautiful CodeGo.net and a brilliant scripting language at your disposal? In fact, this is a more general question: when you program in almost any language, why do you think that when you have Jython and IronPython at your disposal, an ugly, bad scripting language?
Therefore, to sum up: the practice of DI/IOC is equally important in Python, because it is in Java, for exactly the same reason. The DI/IOC, however, is based on language and often disappears so lightly. There is a metaphor here: in assembly, subroutine call is a considerable transaction-you have to save your local variables and registers, save the instruction pointer of the return address and change it to the subroutine you called, and arrange it to jump back to your subroutine when it is completed, so that the called party there can find them, etc. IOW: In assembly, "subroutine call" is a design pattern, which is similar to Fortran language before, and some of them are built in subroutine call. People build their own "subroutines". You will say that subroutines are called in Python, but you don't "subroutines". BTW: Take an example of what it looks like to see Girard's Bracha Newspeak programming language and the theme of his book:
Constructors are harmful
Injection of death penalty
It is forbidden to import (continued)
2. < You can get a kind of "single" free, just import it from a module. Define a module in the actual instance of an object, and then any client-side code can be imported and really get a job to build/populate the object comprehensively.
this is relative to Java, where you don't import the actual situation of the object. You always have to instantiate them yourself (sorted IoC/DI style method). You can reduce the trouble of instantiating everything without the static (or actual factory class) you own, but you still incur the cost of actually creating new resources.
3.
The control of Django is reversed. For example, the database server is selected as the configuration file, and then the database client side of the corresponding database package instance is provided.
the difference is that Python has a first-class type. Data types, including classes, are their own objects. If you want a specific class, just this class. For example:
ifconfig _ DBMS _ name = =' PostgreSQL':
importpsycopg
self. database _ interface = psycopg
elifconfig _ DBMS _ name = =' MySQL':
...
The later code can Create a database interface through a written form:
my _ db _ connection = self. database _ interface ()
# dostuff with database.
Instead, the function of the template factory, the needs of Java and C ++, Python makes it one or two lines with ordinary code. This is the power of functional imperative programming.
4.
In my opinion, such thing as dependency injection is rigid. When the code is too heavy to change easily, you will find that you have to pick a small part, define interfaces for them, and then let people change their behavior symptoms through the objects that should be inserted into these interfaces. All this is good, but it's best to avoid the first kind.
This is also a symptom of a statically typed language. When the only tool you want to express abstraction is inheritance, then this is almost everywhere. Having said that, C ++ is very similar, but it has never been done by Java developers who are obsessed with builders and interfaces everywhere. It's easy to get the dream of being over-exuberant and being able to write too much universal code with little real benefit. I think this is a cultural thing.
In general, I think Python people choose the right tool to complete the work, which is a coherent and simple whole, not a real tool (a possible plug-in), but it provides a dazzling array of possible configurations. There are still interchangeable parts when necessary, but there is no need to define a fixed interface, because of the relatively simple flexibility of duck typing and language.
5.
Python of haven has been around for several years, but I would say that it is more important than anything because it is a dynamic type of language. For a simple example, in Java, if I want to test writing to the standard output, I DI appropriately and pass any PrintStream to capture the written text and verify it. When I work in Ruby, however, I can dynamically replace the "put option" method in the standard output for verification, leaving out the pictures. If the only reason I create an abstraction is to test the class, "It (think of file system operation or the clock in Java), then directly invest in the solution created by/IoC.
6.
I came back to "Joerg? W Mittag "replied:" Python of DI/IOC has disappeared so lightly ".
to back this up, take a look at the example in Martin Fowler and transplant it from Java to Python:Python's: design _ patterns: inversion _ of _ control
As you can see from the link above, A "container" can be written as 8 lines of code in Python:
classcontainer:
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
7.
IOC and ruby, This is my idea, why hasn't it been widely circulated
Update:
I don't support that website anymore, and the link doesn't work, but you can read it here
8.
Actually, it's easy to write clean enough code and DI (I don't know, will it be stopped by/Python, then, but anyway:)). For example, I actually code perefer like this:
defpolite (name _ str):
return "dear"+name _ str
defrude (a):
return name _ str+",you, moron"
defgrey (name _ str, call =
_
> > greet("Peter")
Hello, dear Peter!
> > greet("Jack", rude)
Hello, Jack, you, moron!
yes, it can be thought that the function/class is just a simple form, but it does work. So, maybe the battery that comes with Python by default is enough here.
p.s. I also post a bigger example of this naive approach in dynamic evaluation of simple Boolean logic in Python.