[Python-3000] Fixing super anyone?

Joel Bender jjb5 at cornell.edu
Wed Apr 25 15:21:28 CEST 2007


I mucked around with this, and here is my version:

     class _super(property):
         def __init__(self):
             property.__init__(self, self.get_super, None, None)

         def get_super(self, klass):
             def dong(obj):
                 class wrapper:
                     def __getattr__(self, fn):
                         save = obj.__class__
                         obj.__class__ = klass.__mro__[1]
                         f = getattr(obj, fn)
                         obj.__class__ = save
                         return f

                 return wrapper()

             return dong

     class _superable(type):
         __super__ = _super()

     class W(object):
         __metaclass__ = _superable

         def f(self):
             print "W"

     class X(W):
         def f(self):
             print "X"
             X.__super__(self).f()

     class Y(W):
         def f(self):
             print "Y"
             Y.__super__(self).f()

     class Z(X, Y):
         def f(self):
             print "Z"
             Z.__super__(self).f()

I would rather the end result look more like this:

     class Z(X, Y):
         def f(self):
             print "Z"
             Z.__super__.f(self)

But I haven't figured out how to do that yet, and the self.__super__.f() 
pattern that Steven Bethard posted trumps it anyway!


Joel



More information about the Python-3000 mailing list