I found a very good video which explains the situation where we would actually go for this pattern. It is targeted to php audiences, but those who understand the basic OO principles can follow it easily.
Now python being a dynamic language the term dependency injection isn't as popular as it is in c# or java. This being because, python is a dynamic language. You assume can link to any bit of code at runtime and execute that functionality. If you have spawned up an instance of the python interpreter, you can write functions on the fly and execute them.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Python 2.7.3 (default, Apr 20 2012, 22:44:07) | |
[GCC 4.6.3] on linux2 | |
Type "help", "copyright", "credits" or "license" for more information. | |
>>> def print_hello(): | |
... print 'Hello foo world' | |
... | |
>>> print_hello() | |
Hello foo world | |
>>> #some other arbitary function call | |
... #will result in an error | |
... foo() | |
Traceback (most recent call last): | |
File "<stdin>", line 3, in <module> | |
NameError: name 'foo' is not defined | |
>>> |
Python also has the notions of modules: they are mechanisms to enforce some modular approach to programming. This is also a way to accomplish reuse. You can write some functionality in them and import them into your main script or the interpreter instance, and use that functionality right away. In fact a lot of what is in the python standard library is reused this way.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Python 2.7.3 (default, Apr 20 2012, 22:44:07) | |
[GCC 4.6.3] on linux2 | |
Type "help", "copyright", "credits" or "license" for more information. | |
>>> import os | |
>>> os.getcwd() | |
'/home/deostroll' | |
>>> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ cd ~/scripts/tmp | |
$ touch m1.py | |
$ pico m1.py | |
$ cat m1.py | |
print 'Imported module m1' | |
def m1foo(): | |
print 'executed m1 foo' | |
$ python | |
Python 2.7.3 (default, Apr 20 2012, 22:44:07) | |
[GCC 4.6.3] on linux2 | |
Type "help", "copyright", "credits" or "license" for more information. | |
>>> import m1 | |
Imported module m1 | |
>>> m1.m1foo() | |
executed m1 foo | |
>>> |
I have my own implementation of dependency injection hosted on github right now. It is inspired from spring.net library (.net library for di). But before you look at the code, and, if you are still unsure of how exactly python import statements work, you should look here.
Looking forward to interact with people on my google plus profile. Happy programming.