Second argument to super().

Steve Holden steve at holdenweb.com
Thu Mar 10 09:59:21 EST 2005


John Roth wrote:
> "Tobiah" <toby at rcsreg.com> wrote in message 
> news:1110396167.53bd67fed97c9e826a92b284283b1455 at teranews...
> 
>> What is the purpose of the second argument to super()?
> 
> 
> I've always  found the docs to be fairly confusing.
> They didn't give me enough context to tell what
> was going on. I also find the terminology confusing:
> "type" seems to mean "new style class object", and
> "object" seems to mean "instance."
> 
I agree that the docs could probably do with some improvement here, but 
this is mostly because (I suspect) the type-based material has been 
shoehorned in to the existing documentation structure. My own suspicion 
was that a more radical revision would yield a better manual, but it 
doesn't look as though anybody has  had time to attempt it.

Certainly super() is some of the deepest magic related to the new-style 
object hierarchy.

> What happens with the second operand is a bit of
> sleight of hand. The object returned from super()
> gives you access to the methods on the next level up the
> mro, however when you use it to invoke a method,
> then the 'self' passed to that method is the second
> object, not the instance returned from super().
> 
This is a key point. I wonder whether we might use this thread to draft 
a patch to the docs for submission on SourceForge?

> In most cases, this is exactly what you want, since if
> the superclass method makes any changes to the
> instance, you  want to be able to see them after the
> call completes.
> 
Quite. It's important that super() doesn't create a completely new 
object, because it's really about identifying an appropriate point in 
the inheritance hierarchy (method resolution order) and offering 
namespace access from that point up, rather than from the base instance 
given as the second argument to super(). This means that the same code 
can be included in many different namespace hierarchies and still work 
correctly in them all.

>> What is meant by the returning of an 'unbound' object
>> when the argument is omitted.
> 
> 
> This is basically for calling static methods. Since a static
> method is not passed an instance, you need a version of
> the object returned from super() that doesn't bind the
> method to an instance.
> 
> There is also the possibility that you might really want
> to call an instance or class method as an unbound method,
> explicitly passing it the instance. This is the reason that
> the object returned from super() can't make the  distinction
> automatically by simply checking for a static method.
> 
>> Also, when would I pass an object as the second argument,
>> and when would I pass a type?
> 
> 
> You need to pass the class object when you're calling
> a class method. While __new__ is technically a static
> method, for most practical purposes you can regard
> it as a class method.
> 
This is all good stuff. We should try to make sure the documentation 
gets enhanced.

regards
  Steve




More information about the Python-list mailing list