Re: using super
- From: Steven D'Aprano <steve@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Mon, 31 Dec 2007 14:08:43 -0000
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
I am <__main__.A object at 0xb7cf676c>a = A()
a.foo(5)
5
This is B!!!b = B()
b.foo(5)
I am <__main__.B object at 0xb7cf68ac>
6
--
Steven
.
- Follow-Ups:
- Re: using super
- From: Gabriel Genellina
- Re: using super
- From: Scott David Daniels
- Re: using super
- References:
- using super
- From: iu2
- using super
- Prev by Date: if you want do business on the internet .pls go,if you want buy much popular and inexpensive price pls go
- Next by Date: Python-URL! - weekly Python news and links (Dec 31)
- Previous by thread: using super
- Next by thread: Re: using super
- Index(es):
Relevant Pages
|