0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Dependency Injector for Python: provider 内で別のオブジェクトを生成

Last updated at Posted at 2022-08-06

Dependency Injector — Dependency injection framework for Python

オブジェクトの生成方法を Module で定義し、その中で別のオブジェクトを自動生成したい場合、provider 関数に injector を渡せば良い。

from injector import Injector, Module, provider


class AppModule(Module):
    @provider
    def provide_hoge(self, injector: Injector) -> Hoge:
        return Hoge(injector.get(Fuga), "hello")


injector = Injector([AppModule])
hoge = injector.get(Hoge)
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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?