[Python-ideas] Composition over Inheritance
rosuav at gmail.com
Sat Oct 28 21:57:13 EDT 2017
On Sun, Oct 29, 2017 at 12:46 PM, Soni L. <fakedme+py at gmail.com> wrote:
> But you should be able to do things like
> car = object()
> car[Engine] = SimpleEngine()
> car.[Engine].kickstart() # calls kickstart method with an instance of
> SimpleEngine as `self`/first argument and `car` as second argument.
> # etc
> Which your decorator-based approach quite obviously doesn't let you.
I think I follow what you're trying to do here. You want to have a way
to refer to a subobject while letting it know about the parent. We
already have something like that: when you call a function that was
attached to a class, it gets to know which instance of that class you
used to locate that function. Maybe there's a way to use descriptor
protocol for this too?
def kickstart(self, caller):
"""*boot* Engine starts"""
Engine = magic(SimpleEngine)
car = Car()
When you look up car.Engine, it remembers a reference to car. Then you
look up any callable from there, and it automatically provides an
additional parameter. I'm not sure how the details would work, but in
theory, this should be possible, right?
More information about the Python-ideas