python-dependency-injector/docs/advanced_usage.rst

39 lines
966 B
ReStructuredText
Raw Normal View History

2015-04-02 16:17:00 +03:00
Advanced usage
==============
2015-04-18 22:45:51 +03:00
Below you can find some variants of advanced usage of *Objects*.
2015-04-02 16:17:00 +03:00
@inject decorator
-----------------
2015-04-02 23:46:53 +03:00
``@inject`` decorator could be used for patching any callable with injection.
2015-04-18 22:45:51 +03:00
Any Python object will be injected *as is*, except *Objects* providers,
2015-04-02 16:17:00 +03:00
that will be called to provide injectable value.
.. code-block:: python
"""`@inject` decorator example."""
from objects.providers import NewInstance
from objects.injections import KwArg
from objects.injections import inject
new_object = NewInstance(object)
@inject(KwArg('object_a', new_object))
@inject(KwArg('some_setting', 1334))
def example_callback(object_a, some_setting):
"""This function has dependencies on object a and b.
Dependencies are injected using `@inject` decorator.
"""
assert isinstance(object_a, object)
assert some_setting == 1334
example_callback()
example_callback()