Skip to content

Instantly share code, notes, and snippets.

@allenyang79
Created October 8, 2019 07:48
Show Gist options
  • Save allenyang79/a00ed3bbdac278f9e13c814403655aa7 to your computer and use it in GitHub Desktop.
Save allenyang79/a00ed3bbdac278f9e13c814403655aa7 to your computer and use it in GitHub Desktop.
from injector import Injector, Key, inject
Sizes = Key('sizes')
Names = Key('names')
class A:
@inject
def __init__(self, number: int, names: Names, sizes: Sizes):
print("init A", [number, names, sizes])
self.number = number
self.names = names
self.sizes = sizes
def __str__(self):
return f"A<{self.number}, {self.names}, {self.sizes}>"
def configure(binder):
binder.bind(A)
binder.bind(int, to=123)
binder.bind(Names, to='Bob')
binder.bind(Sizes, to=[1, 2, 3])
a = Injector(configure).get(A)
# [123, 'Bob', [1, 2, 3]]
print(a)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment