Re: using super



On Mon, 31 Dec 2007 05:47:31 -0800, iu2 wrote:

Hi

I'm trying to make a method call automatically to its super using this
syntax:

[snip code]


I'm not sure if this is your only problem or not, but super() only works
with new-style classes, not with classic classes. You must inherit from
object, or it cannot possibly work.

Change "class A" to "class A(object)".

However, I suspect your approach may be too complicated. Try this:


def chain(meth): # A decorator for calling super.
def f(self, *args, **kwargs):
result = meth(self, *args, **kwargs)
S = super(self.__class__, self)
getattr(S, meth.__name__)(*args, **kwargs)
return result
f.__name__ = "chained_" + meth.__name__
return f



class A(object):
def foo(self, x):
print "I am %s" % self
return x

class B(A):
@chain
def foo(self, x):
print "This is B!!!"
return x + 1



a = A()
a.foo(5)
I am <__main__.A object at 0xb7cf676c>
5
b = B()
b.foo(5)
This is B!!!
I am <__main__.B object at 0xb7cf68ac>
6



--
Steven
.



Relevant Pages