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

Thomas Wouters thomas at python.org
Wed Apr 19 16:17:49 CEST 2006


On 4/19/06, Guido van Rossum <guido at python.org> wrote:
>
>
> > >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.
> >
> > If super.foobar(args) -> super(ThisClass,firstarg).foobar(args), then it
> > will Just Work(TM) for class methods, since their first argument is the
> > class, and super(someclass,myclass).aClassMethod(...) is the normal way
> of
> > invoking a superclass classmethod.
>
> But how about static methods? Inside a static method, I believe you're
> allowed to write
>
>   super(ThisClass).foobar(args)
>
> which would call
>
>   ThisClass.__base__.foobar(args)
>
> (assuming single inheritance for a moment) i.e. nothing is added to
> the argument list.


That's not how it works (and that's good; refusing the temptation to guess,
and what not :) super() cannot make any assumptions about the next class in
the MRO, and since you don't *have* a class in a staticmethod, it has no way
to find out. staticmethods just can't sensibly call 'their baseclass method'
(unless they're really just "explicit classmethods", like __new__, but those
best be real classmethods (even __new__ :))

Currently, super(ThisClass) returns an 'unbound super object', which is not
a proxy for 'the next class' but rather an descriptor that would get the
right proxy object upon retrieval:

>>> class X(object):
...    @staticmethod
...    def static(*args):
...        print "X.static%r" % (args,)
...
>>> class Y(X):
...    @staticmethod
...    def static(*args):
...        print "Y.static%r" % (args,)
...        super(Y).static(*args)
...
>>> Y.static()
Y.static()
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "<stdin>", line 5, in static
AttributeError: 'super' object has no attribute 'static'
>>> y = Y()
>>> y.static()
Y.static()
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "<stdin>", line 5, in static
AttributeError: 'super' object has no attribute 'static'

And to prove it's a descriptor:
>>> class Z(X):
...     @staticmethod
...     def static(*args):
...         print "Z.static%r" % (args,)
...         super(Z).__get__(Z, None).static(*args)
...
>>> Z.static()
Z.static()
X.static()
>>> Z().static()
Z.static()
X.static()

Doing super(Z).__get__(Z, None).static is just as static as calling X.static,
though (with the only difference being that you name Z multiple times,
instead of naming X multiple times.) It doesn't take MI into account at all.

And yes, staticmethods are damned near useless. Let's not worry about them
calling their baseclass methods -- they honestly shouldn't.

--
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/950a71b3/attachment.html 


More information about the Python-3000 mailing list