2015-07-16 00:48:30 +03:00
|
|
|
Callable providers
|
|
|
|
------------------
|
|
|
|
|
2015-11-23 16:39:19 +03:00
|
|
|
.. module:: dependency_injector.providers
|
|
|
|
|
|
|
|
:py:class:`Callable` provider is a provider that wraps particular callable with
|
2015-07-16 00:48:30 +03:00
|
|
|
some injections. Every call of this provider returns result of call of initial
|
|
|
|
callable.
|
|
|
|
|
2015-07-20 19:31:31 +03:00
|
|
|
Callable providers and injections
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
2015-11-23 16:39:19 +03:00
|
|
|
:py:class:`Callable` takes a various number of positional and keyword
|
|
|
|
arguments that are used as decorated callable injections. Every time, when
|
|
|
|
:py:class:`Callable` is called, positional and keyword argument injections
|
|
|
|
would be passed as an callable arguments.
|
2015-10-22 16:51:16 +03:00
|
|
|
|
|
|
|
Such behaviour is very similar to the standard Python ``functools.partial``
|
|
|
|
object, except of one thing: all injectable values are provided
|
2015-11-23 16:39:19 +03:00
|
|
|
*"as is"*, except of providers (subclasses of :py:class:`Provider`). Providers
|
2015-10-22 16:51:16 +03:00
|
|
|
will be called every time, when injection needs to be done. For example,
|
2015-11-23 16:39:19 +03:00
|
|
|
if injectable value of injection is a :py:class:`Factory`, it will provide
|
|
|
|
new one instance (as a result of its call) every time, when injection needs
|
|
|
|
to be done.
|
2015-10-22 16:51:16 +03:00
|
|
|
|
2015-11-23 16:39:19 +03:00
|
|
|
:py:class:`Callable` behaviour with context positional and keyword arguments
|
|
|
|
is very like a standard Python ``functools.partial``:
|
2015-10-22 16:51:16 +03:00
|
|
|
|
2015-11-23 16:39:19 +03:00
|
|
|
- Positional context arguments will be appended after :py:class:`Callable`
|
2015-10-22 16:51:16 +03:00
|
|
|
positional injections.
|
2015-11-23 16:39:19 +03:00
|
|
|
- Keyword context arguments have priority on :py:class:`Callable` keyword
|
2015-10-22 16:51:16 +03:00
|
|
|
injections and will be merged over them.
|
|
|
|
|
2015-11-23 16:39:19 +03:00
|
|
|
Example that shows usage of :py:class:`Callable` with positional argument
|
2015-10-22 16:51:16 +03:00
|
|
|
injections:
|
|
|
|
|
|
|
|
.. literalinclude:: ../../examples/providers/callable_args.py
|
|
|
|
:language: python
|
2015-07-16 00:48:30 +03:00
|
|
|
|
2015-11-23 16:39:19 +03:00
|
|
|
Next one example shows usage of :py:class:`Callable` with keyword argument
|
2015-10-22 16:51:16 +03:00
|
|
|
injections:
|
2015-07-16 00:48:30 +03:00
|
|
|
|
2015-07-25 00:51:14 +03:00
|
|
|
.. image:: /images/providers/callable.png
|
2015-07-16 00:48:30 +03:00
|
|
|
:width: 100%
|
|
|
|
:align: center
|
|
|
|
|
2015-10-22 16:51:16 +03:00
|
|
|
.. literalinclude:: ../../examples/providers/callable_kwargs.py
|
2015-08-03 15:49:55 +03:00
|
|
|
:language: python
|
2015-07-20 19:31:31 +03:00
|
|
|
|
|
|
|
Callable providers delegation
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
2015-11-23 16:39:19 +03:00
|
|
|
:py:class:`Callable` provider could be delegated to any other provider via any
|
|
|
|
kind of injection. Delegation of :py:class:`Callable` providers is the same as
|
|
|
|
:py:class:`Factory` and :py:class:`Singleton` providers delegation, please
|
|
|
|
follow *Factory providers delegation* section for example.
|
2015-07-20 19:31:31 +03:00
|
|
|
|
2015-11-23 16:39:19 +03:00
|
|
|
:py:class:`Callable` delegate could be created obviously using
|
|
|
|
``Delegate(Callable(...))`` or by calling ``Callable(...).delegate()`` method.
|
2015-07-20 19:31:31 +03:00
|
|
|
|
|
|
|
Example:
|
|
|
|
|
2015-08-03 15:49:55 +03:00
|
|
|
.. literalinclude:: ../../examples/providers/callable_delegation.py
|
|
|
|
:language: python
|