[Tutor] super() with Multiple Inheritance

Steven D'Aprano steve at pearwood.info
Fri Apr 15 03:06:37 CEST 2011


Alan Gauld wrote:
> 
> "James Thornton" <james at jamesthornton.com> wrote
> 
>> I found this issue -- I was setting setting self.s to the return value
>> of super() and trying to use self.s in params():
> 
>> ...but this won't work.
> 
> No, because super returns whatever the superclass
> method returns. Which in init() is usually None.

No, super returns a proxy object that encapsulates knowledge of the 
superclasses of the argument (usually "self", but not necessarily).

 >>> class A(object):
...     attr = "Nobody expects the SPANISH INQUISITION!"
...
 >>> class B(A):
...     attr = "something else"
...
 >>> b = B()
 >>> s = super(B, b)
 >>> s
<super: <class 'B'>, <B object>>


What you do with that proxy object is then lookup attributes (usually 
methods, but data attributes are fine too):


 >>> s.attr
'Nobody expects the SPANISH INQUISITION!'

If you look up a method, and *call* that method, then of course the 
complete chain of super::method lookup::method call will return whatever 
the method itself returns. But that's different from saying that super 
itself returns (say) None.




-- 
Steven



More information about the Tutor mailing list