[Python-3000] A super() idea - no _getframe() involved

Thomas Wouters thomas at python.org
Wed Apr 19 12:15:17 CEST 2006


On 4/19/06, Guido van Rossum <guido at python.org> wrote:
>
> On 4/19/06, Phillip J. Eby <pje at telecommunity.com> wrote:
> > Here's how you solve the "how does super() get the current class"
> problem,
> > using existing compiler and VM constructs, and without relying on class
> > names, or on functions not being decorated, or anything like that.  And
> > it's so simple you'll slap your forehead for not thinking of it
> first.  :)
>
> Actually, I *did* think of it first. :-)
>
>     http://mail.python.org/pipermail/python-3000/2006-April/000947.html


And yet before that:
http://mail.python.org/pipermail/python-3000/2006-April/000922.html

Needless to say, I'm in favour, if it can be swung ;-)

Oh, I believe super() also supports static and/or class methods. I'm
> not sure how to handle this but I'm sure you can think of something.


It should just use the descriptor magic to retrieve the right method object
from the stored 'current class', passing the class and instance (if any) as
usual. That does mean, though, that super (however you want to spell it)
needs to be informed what the instance or class is. The current super() call
is explicitly passed 'self' (or 'cls' for classmethods), but it has no way
of operating in staticmethods unless they are really classmethods in
disguise (like __new__, a staticmethod that gets the class as first
argument.) since, with neither 'self' nor 'cls', there is no way for super
to figure out the right MRO.

So, super would have to be used like so:

 def meth(self, arg, kwarg=kwval):
     return super.meth(self, arg, kwarg=kwval)
 @classmethod
 def cmeth(cls, arg, kwarg=kwval):
     return super.cmeth(cls, arg, kwarg=kwval)
 @staticmethod
 def smeth(cls, arg, kwarg=kwval):
     # cls is not necessarily the right class, but it's the best guess
     return super.smeth(cls, arg, kwarg=kwval)

Which looks quite a bit like the old BaseClass.meth(self, ...) calls. I
think it's a pity we can't make it look more like normal methodcalls, maybe
by passing the class or instance explicitly: super(self).meth(arg,
kwargv=kwval) (and super(cls) for classmethods.) Unfortunately, that is
entirely and silently and somewhat surprisingly incompatible with Python 2.x,
where super(cls) creates an unbound super proxy.

--
Thomas Wouters <thomas at python.org>

Hi! I'm a .signature virus! copy me into your .signature file to help me
spread!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/python-3000/attachments/20060419/b421d6ab/attachment.html 


More information about the Python-3000 mailing list