[Python-3000] Fixing super anyone? (original) (raw)

Guido van Rossum guido at python.org
Wed Apr 25 20:33:59 CEST 2007


On 4/24/07, Steven Bethard <steven.bethard at gmail.com> wrote:

class Super(object): def init(self, type, obj=None): if isinstance(obj, Super): obj = obj.obj self.type = type self.obj = obj def get(self, obj, cls=None): if obj is None: raise Exception('only supports instances') else: return Super(self.type, obj) def getattr(self, attr): mro = iter(self.obj.class.mro) for cls in mro: if cls is self.type: break for cls in mro: if attr in cls.dict: x = cls.dict[attr] if hasattr(x, 'get'): x = x.get(self, cls) return x raise AttributeError, attr

class autosuper(type): def init(cls, name, bases, clsdict): cls.super = Super(cls) class A: metaclass = autosuper def f(self): return 'A' class B(A): def f(self): return 'B' + self.super.f() class C(A): def f(self): return 'C' + self.super.f() class D(B, C): def f(self): return 'D' + self.super.f() assert D().f() == 'DBCA'

But:

class E(D): pass

print E().f()

This prints DDBCA which surely isn't right.

Sounds like the classic bug in such attempts.

-- --Guido van Rossum (home page: http://www.python.org/~guido/)



More information about the Python-3000 mailing list