[Python-Dev] Classes and Metaclasses in Smalltalk
Thomas Heller
thomas.heller@ion-tof.com
Wed, 2 May 2001 16:49:02 +0200
> thomas wrote:
>
> > > why not spell it out:
> > >
> > > self.__super__.foo(arg1, arg2)
> > >
> > > or
> > >
> > > self.super.foo(arg1, arg2)
> > >
> > > or
> > >
> > > super(self).foo(arg1, arg2)
> >
> > IMO we still need to specify the class, and there we are:
> >
> > super(self, MyClass).foo(arg1, arg2)
>
> isn't that the same as self.__class__ ? in which case
> super is something like:
>
> import new
>
> class super:
> def __init__(self, instance):
> self.instance = instance
> def __getattr__(self, name):
> for klass in self.instance.__class__.__bases__:
> member = getattr(klass, name, None)
> if member:
> if callable(member):
> return new.instancemethod(member, self.instance, klass)
> return member
> raise AttributeError(name)
>
No, it's not the same. Consider:
class X:
def test(self):
print "test X"
class Y(X):
def test(self):
print "test Y"
super(self).test()
class Z(Y):
pass
X().test()
print
Y().test()
print
Z().test()
print
This prints:
test X
test Y
test X
test Y
test Y
(more test Y lines deleted)
Runtime error: maximum recursion depth exceeded
This is because super(self).test for the Z() object
should start the search in the X class, not in the Y class.
Thomas