Reputation: 8298
I read quite a lot of material about the correct use of super
with and without argument(s) but there is still something that I do not understand.
Let's consider the following example:
class A:
def foo(self):
pass
class B(A):
pass
This is the part I understand:
super(B, B()) # <bound method A.foo of <__main__.B object at ...>>
which makes sense, as the two argument version of super
, with the second argument being an object returns a bound object.
Let's say that I want to access the function in A
, not the bound method. I would try this:
super(B).foo # AttributeError: 'super' object has no attribute 'foo'
Which is the part that I do not understand.
Trying to "trick" super
in the following way works though:
super(B, B).foo # <function __main__.A.foo(self)>
What am I missing?
Upvotes: 2
Views: 90
Reputation: 531215
super
returns a proxy to some object. To figure out which object you want to proxy, you need to see how you would get a reference to the function without using super
.
If a
is an instance of A
, then a.foo
gives you the return value of A.__dict__['foo'].__get__(a, A)
, which is an instance of method
. To get the function itself, you use A.foo
, which is the return value of A.__dict__['foo'].__get__(None, A)
.
This suggests you need a proxy for the class, not the instance of the class, which is why you pass A
, rather than an instance of A
, as the second argument to super
.
Upvotes: 1