LoginSignup
0
0

More than 1 year has passed since last update.

Dependency Injector for Python: 配列を Inject する

Last updated at Posted at 2022-08-06

Dependency Injector — Dependency injection framework for Python

オブジェクト単体でなく配列を注入したい場合、multiprovider を使う。

from injector import Injector, Module, multiprovider


class AppModule(Module):
    @multiprovider
    def provide_hoges(self, injector: Injector) -> list[Hoge]:
        return [
            injector.get(Hoge1),
            injector.get(Hoge2),
        ]


injector = Injector([AppModule])
hoges = injector.get(list[Hoge])

注意点

  • list は Python 3.9 からサポートされた構文であり、過去のバージョンでも from __future__ import annotations を記述すればそれ自体を使用することはできるが、inject はできない。その場合は代わりに typing.List を使う。
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0