[Python-3000] Fixing super anyone?

Steven Bethard steven.bethard at gmail.com
Tue Apr 24 06:22:44 CEST 2007


On 4/23/07, Calvin Spealman <ironfroggy at gmail.com> wrote:
> I will +1 on the self.__super__ suggestion. Hey, its very doable and I
> even whipped it up with a simple metaclass, so it would be a tiny
> change to 'type' in order to actually implement it as a standard
> feature. The demonstration is as follows:
>
> class _superdesc(object):
>     def __get__(self, obj, objcls):
>         return super(cls, obj)
>
> class autosuper(type):
>     def __init__(cls, name, bases, clsdict):
>         cls.__super__ = _superdesc()
>
> class A(object):
>     __metaclass__ = autosuper
>     x = 1
>
> class B(A):
>     x = 2
>
> assert B().__super__.x == 1

Does that really work?  There's a typo in _superdesc (I don't know
where 'cls' comes from) but if you meant 'objcls', here's what I get::

    >>> class _superdesc(object):
    ...     def __get__(self, obj, cls):
    ...         return super(cls, obj)
    ...
    >>> class autosuper(type):
    ...     def __init__(cls, name, bases, clsdict):
    ...         cls.__super__ = _superdesc()
    ...
    >>> class A:
    ...     __metaclass__ = autosuper
    ...     def f(self):
    ...         print 'A'
    ...
    >>> class B(A):
    ...     def f(self):
    ...         print 'B'
    ...         self.__super__.f()
    ...
    >>> class C(A):
    ...     def f(self):
    ...         print 'C'
    ...         self.__super__.f()
    ...
    >>> class D(B, C):
    ...     def f(self):
    ...         print 'D'
    ...         self.__super__.f()
    ...
    >>> D().f()
    D
    B
    B
    B
    ...
    Traceback (most recent call last):
      File "<interactive input>", line 1, in <module>
    ...
    RuntimeError: maximum recursion depth exceeded

STeVe
-- 
I'm not *in*-sane. Indeed, I am so far *out* of sane that you appear a
tiny blip on the distant coast of sanity.
        --- Bucky Katt, Get Fuzzy


More information about the Python-3000 mailing list