[Python-3000] Fixing super anyone?
Tim Delaney
tcdelaney at optusnet.com.au
Wed Apr 25 22:29:43 CEST 2007
From: "Guido van Rossum" <guido at python.org>
> class E(D): pass
>
> print E().f()
>
> This prints DDBCA which surely isn't right.
>
> Sounds like the classic bug in such attempts.
Yep - missing overridden methods tend to either do the above, or end up in
infinite recursion. My bytecode hacking version doesn't suffer from these
problems ...
>>> class autosuper(object):
... __metaclass__ = _autosuper
...
>>> class A(autosuper):
... def f(self):
... print 'A:', super
...
>>> class B(A):
... def f(self):
... print 'B:', super
... super.f()
...
>>> class C(A):
... def f(self):
... print 'C:', super
... super.f()
...
>>> class D(B, C):
... pass
...
>>> class E(D):
... def f(self):
... print 'E:', super
... super.f()
...
>>> class F(E, A):
... pass
...
>>> F().f()
E: <super: <class 'E'>, <F object>>
B: <super: <class 'B'>, <F object>>
C: <super: <class 'C'>, <F object>>
A: <super: <class 'A'>, <F object>>
What I haven't worked out yet is if you should be able to do the following:
class A(autosuper):
def f(self):
print 'A:', super
class B(A):
def f(self):
def inner():
print 'B:', super
super.f()
inner()
Should the call to inner() result in a call to A.f? Currently my bytecode
version doesn't do this. I think this should be addressed in the PEP, as
well as my proposal to have super(args) work as super.func(args) when called
inside func.
Tim Delaney
More information about the Python-3000
mailing list