from py3njection import inject
from some_package import ClassToInject
class Demo:
@inject
def __init__(self, object_to_use: ClassToInject):
self.dependency = object_to_use
demo = Demo()
The decorator @inject looks for any annotated method/function parameters (return annotation excluded).
It creates a new object from the specified class if no object is already provided.
@singleton
class ClassToInject:
pass
That's it ! When @inject sees a class with this decorator, it always uses the same instance.
It's available on PyPI !
pip install py3injection
Or get it at : https://pypi.python.org/pypi/py3njection
A more complete documentation is available here : http://py3njection.readthedocs.org/en/latest/
Feel free to contribute in any way :
- help, bugs, issues, suggestions : https://github.com/Aigrefin/py3njection/issues
- twitter account : https://twitter.com/Julien_Tellier
- and finally my mail address is in the setup.py
- Unit tests come easy to set up (unless you have too many dependencies, but that would be a code smell, right ?). Just specify mock instances at your object creation. Examples will come later.
- This also means the injected object could also have some of its members injected too at their initialization !