mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-22 17:47:02 +03:00
1ad852d193
* Update index page * Update providers index page * Make a little wording fix on containers index page * Refactor factory provider docs header * Update factory injection docs * Update factory init injections example and picture * Start work on underlying providers * Finish the docs for factory arguments to the underlying providers * Edit providers delegation section * Edit section about specialized factory provider * Edit abstract factory section * Edit FactoryAggregate docs * Add meta keywords and description
37 lines
785 B
Python
37 lines
785 B
Python
"""`Factory` providers init injections example."""
|
|
|
|
from dependency_injector import providers
|
|
|
|
|
|
class Photo:
|
|
...
|
|
|
|
|
|
class User:
|
|
def __init__(self, uid: int, main_photo: Photo) -> None:
|
|
self.uid = uid
|
|
self.main_photo = main_photo
|
|
|
|
|
|
photo_factory = providers.Factory(Photo)
|
|
user_factory = providers.Factory(
|
|
User,
|
|
main_photo=photo_factory,
|
|
)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
user1 = user_factory(1)
|
|
# Same as: # user1 = User(1, main_photo=Photo())
|
|
|
|
user2 = user_factory(2)
|
|
# Same as: # user2 = User(2, main_photo=Photo())
|
|
|
|
# Context keyword arguments have a priority:
|
|
another_photo = Photo()
|
|
user3 = user_factory(
|
|
uid=3,
|
|
main_photo=another_photo,
|
|
)
|
|
# Same as: # user3 = User(uid=3, main_photo=another_photo)
|