Comments (3)
Yes, this is possible.
from injector.
Thanks
On Mon, Oct 24, 2016 at 2:08 AM, Alec Thomas [email protected]
wrote:
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
#56 (comment), or mute
the thread
https://github.com/notifications/unsubscribe-auth/AAPFsrmypCowwmFYzsz7V2QRbI6BP1rEks5q2_cDgaJpZM4KWrMs
.
Kind Regards,
Tadas Šubonis
from injector.
I'll leave this here for historical visibility - subclassing is another way to achieve what you want:
from injector import Binder, inject, Injector
class Seat:
def __init__(self, data: str) -> None:
self.data = data
def __repr__(self) -> str:
return 'Seat(%r)' % (self.data,)
class DriverSeat(Seat):
pass
class PassengerSeat(Seat):
pass
class Car:
@inject
def __init__(self, driver_seat: DriverSeat, passenger_seat: PassengerSeat) -> None:
self.driver_seat = driver_seat
self.passenger_seat = passenger_seat
def configure(binder: Binder) -> None:
binder.bind(DriverSeat, to=DriverSeat('fancy driver seat'))
binder.bind(PassengerSeat, to=PassengerSeat('crude passenger seat'))
injector = Injector([configure])
car = injector.get(Car)
print(car.driver_seat)
Result:
Seat('fancy driver seat')
Not everyone needs to like this solution (it's a bit hacky) but it works and once Injector gets some type hints it'll be more static analysis friendly than using Key
.
from injector.
Related Issues (20)
- When using AssistedBuilder and child injector at the same time, objects are injected with the parent's setting. HOT 1
- `NoInject` raises when `None` is the default value
- How to use the binder callable to bind to multiple instances at the same time
- Python 3.10 regression: injector.UnknownProvider: couldn't determine provider HOT 17
- Support pep 604 style for optional types (`Type | None`)
- Bind a provider to multiple interfaces?
- Binding multiple interfaces to same implementation HOT 2
- Singletons for parent/child injectors are order-dependent HOT 9
- Maintainer status? HOT 2
- Potential resource leak HOT 3
- Binding of Annotated types does not work as one would expect HOT 8
- Is it an antipattern to have multiple instances of Injector (because of missing Generics support)? HOT 2
- Testing example in docs is not working. ImportError: cannot import name 'with_injector' from 'injector' HOT 6
- FastAPI support HOT 2
- Injection of two abstract classes and annotation
- Question about the example code and intended use HOT 2
- Differentiating between multiple providers HOT 4
- Providing different instances of the same type in the same module HOT 1
- Issue with ClassAssistedBuilder HOT 4
- Implementation of SessionScope Needed
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from injector.