super() and __get__

Michael Hudson mwh at python.net
Wed Nov 27 11:04:14 EST 2002


Mike Dean <klaatu at evertek.net> writes:

> These are probably two separate questions, but...
> 
> I was out on Guido's page on Unifying Types and Classes, trying to grok 
> super(), metaclasses, and th meaning of life.  I think I get this 
> metaclass thing, but let me see if I get this right on super() - all 
> super() does, is create an object that responds to attribute access by 
> passing that access on to the first object (class, type, whatever) in 
> the MRO of the object with which super() was called, starting with the 
> type super() was called with (i.e., if class C has 10 base classes & 
> types, once all flattened out into the MRO, and class B is the 4th in 
> the MRO, then it only searches from B on).  Am I correct?

Probably :)  All I think you have to know about super() is that you write

    def meth(args):
        ...
        super(<class textually enclosing here>, self).meth(args)

For more info, the code is your friend.  Be warned, brain-exploding
dragons live in Objects/typeobject.c, but the implementation of
super() probably isn't that bad.

> Now, a couple more questions...
> 
> Where did this __get__ thing used in describing super() (and another 
> thing or two on that page) come from?

It's new in 2.2 if that's what you're asking.

> I can't seem to find it in the docs anywhere.

descrintro.html is the only documentation on this sort of thing, I
think (there might be some stuff in some of the PEPs, but not much
IIRC).

> It was used in the Python Super() implementation, as well as another
> time or two.
> 
> Also, if object is the base class of type, why does object.__mro__ work 
> when object defines no __mro__?

?  object.__mro__ is just (object,), no?  Don't see the problem with
that.

> And finally, since __mro__ appears to be an attribute merely inherited 
> from type by all classes, if I have a fancy, can I override __mro__ to 
> suit some bizarre purpose?

Um, I think the answer to that is "no".

> Or is it ignored, simply a mechanism to access __mro__?

It's a wrapper around the C level field tp_mro in the type struct.

> Likewise, why does type also have an mro method, when there's
> __mro__?

You can make a class' metaclass have a .mro() method which will be
called during class creation -- this is in descrintro.html isn't it?
Or is it Lib/test/test_descr.py?  Lots of stuff in that last that you
might want to read, anyway.

Cheers,
M.

-- 
  Premature optimization is the root of all evil.
       -- Donald E. Knuth, Structured Programming with goto Statements



More information about the Python-list mailing list