[Python-3000] Fixing super anyone?
Calvin Spealman
ironfroggy at gmail.com
Tue Apr 24 16:11:21 CEST 2007
I must have miscopied then because it worked perfectly here. Yes, I
meant to have the _superdesc defined inside the metaclass __init__,
but thought I could pull it out to make it cleaner. I forgot it
actually had to be there! Here is the metaclass that works.
class autosuper(type):
def __init__(cls, name, bases, clsdict):
class _superdesc(object):
def __get__(self, obj, objcls):
return super(cls, obj)
cls.__super__ = _superdesc()
On 4/24/07, Steven Bethard <steven.bethard at gmail.com> wrote:
> 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
>
--
Read my blog! I depend on your acceptance of my opinion! I am interesting!
http://ironfroggy-code.blogspot.com/
More information about the Python-3000
mailing list