According to the docs, super(cls, obj)
returns
a proxy object that delegates method calls to a parent or sibling class of type cls
I understand why super()
offers this functionality, but I need something slightly different: I need to create a proxy object that delegates methods calls (and attribute lookups) to class cls
itself; and as in super
, if cls
doesn't implement the method/attribute, my proxy should continue looking in the MRO order (of the new not the original class). Is there any function I can write that achieves that?
Example:
class X:
def act():
#...
class Y:
def act():
#...
class A(X, Y):
def act():
#...
class B(X, Y):
def act():
#...
class C(A, B):
def act():
#...
c = C()
b = some_magic_function(B, c)
# `b` needs to delegate calls to `act` to B, and look up attribute `s` in B
# I will pass `b` somewhere else, and have no control over it
Of course, I could do b = super(A, c)
, but that relies on knowing the exact class hierarchy and the fact that B
follows A
in the MRO. It would silently break if any of these two assumptions change in the future. (Note that super
doesn't make any such assumptions!)
If I just needed to call b.act()
, I could use B.act(c)
. But I am passing b
to someone else, and have no idea what they'll do with it. I need to make sure it doesn't betray me and start acting like an instance of class C
at some point.
A separate question, the documentation for super()
(in Python 3.2) only talks about its method delegation, and does not clarify that attribute lookups for the proxy are also performed the same way. Is it an accidental omission?
EDIT
The updated Delegate approach works in the following example as well:
class A:
def f(self):
print('A.f')
def h(self):
print('A.h')
self.f()
class B(A):
def g(self):
self.f()
print('B.g')
def f(self):
print('B.f')
def t(self):
super().h()
a_true = A()
# instance of A ends up executing A.f
a_true.h()
b = B()
a_proxy = Delegate(A, b)
# *unlike* super(), the updated `Delegate` implementation would call A.f, not B.f
a_proxy.h()
Note that the updated class Delegate
is closer to what I want than super()
for two reasons:
super()
only does it proxying for the first call; subsequent calls will happen as normal, since by then the object is used, not its proxy.super()
does not allow attribute access.
Thus, my question as asked has a (nearly) perfect answer in Python.
It turns out that, at a higher level, I was trying to do something I shouldn't (see my comments here).
No, this is not accidental.
super()
does nothing for attribute lookups. The reason is that attributes on an instance are not associated with a particular class, they're just there. Consider the following:Which class "owns"
foo
,bar
, andbaz
?If I wanted to view
instance
as an instance of C, should it have abaz
attribute beforemethod
is called? How about afterwards?If I view
instance
as an instance of A, what value shouldfoo
have? Shouldbar
be invisible because was only added in B, or visible because it was set to a value outside the class?All of these questions are nonsense in Python. There's no possible way to design a system with the semantics of Python that could give sensible answers to them.
__init__
isn't even special in terms of adding attributes to instances of the class; it's just a perfectly ordinary method that happens to be called as part of the instance creation protocol. Any method (or indeed code from another class altogether, or not from any class at all) can create attributes on any instance it has a reference to.In fact, all of the attributes of
instance
are stored in the same place:There's no way to tell which of them were originally set by which class, or were last set by which class, or whatever measure of ownership you want. There's certainly no way to get at "the
A.foo
being shadowed byD.foo
", as you would expect from C++; they're the same attribute, and any writes to to it by one class (or from elsewhere) will clobber a value left in it by the other class.The consequence of this is that
super()
does not perform attribute lookups the same way it does method lookups; it can't, and neither can any code you write.In fact, from running some experiments, neither
super
nor Sven'sDelegate
actually support direct attribute retrieval at all!Then both work as expected for methods:
But:
So they both only really work for calling some methods on, not for passing to arbitrary third party code to pretend to be an instance of the class you want to delegate to.
They don't behave the same way in the presence of multiple inheritance unfortunately. Given:
Then:
Because
Delegate
ignores the full MRO of whatever class_delegate_obj
is an instance of, only using the MRO of_delegate_cls
. Whereassuper
does what you asked in the question, but the behaviour seems quite strange: it's not wrapping an instance of C to pretend it's an instance of B, because direct instances of B don't havefoo
defined.Here's my attempt:
I rely on the
__mro__
attribute of classes to properly figure out where to start from, then I just usesuper
. You could walk the MRO chain from that point yourself checking class__dict__
s for methods instead if the weirdness of going back one step to usesuper
is too much.I've made no attempt to handle unusual attributes; those implemented with descriptors (including properties), or those magic methods looked up behind the scenes by Python, which often start at the class rather than the instance directly. But this behaves as you asked moderately well (with the caveat expounded on ad nauseum in the first part of my post; looking up attributes this way will not give you any different results than looking them up directly in the instance).
This class should cover the most common cases:
Use it like this:
(with the names from your example code.)
Restrictions:
You cannot retrieve some special attributes like
__class__
etc. from the class you pass in the constructor via this proxy. (This restistions also applies tosuper
.)This might behave weired if the attribute you want to retrieve is some weired kind of descriptor.
Edit: If you want the code in the update to your question to work as desired, you can use the foloowing code:
This passes the proxy object as
self
parameter to any called method, and it doesn't need the original object at all, hence I deleted it from the constructor.If you also want instance attributes to be accessible you can use this version: