Decorator question
Steven W. Orr
steveo at syslang.net
Wed May 23 09:46:41 EDT 2007
I just discovered decorators. Very cool. My question is that I can't
figure out how to make a decorator not be restricted to a function so it
would also work on a method.
Here's my code:
def g(expr):
def rpt(func):
def wrapper(t):
for ii in range(expr):
print ii,
func(t)
wrapper.__name__ = func.__name__
wrapper.__dict__ = func.__dict__
wrapper.__doc__ = func.__doc__
return func
return wrapper
return rpt
@g(20)
def f(s):
print 's="%s"'%s
f('Hello')
It works fine, but now I want to apply the same decorator to a class
method.
class KK:
# @g(20) This obviously doesn't work.
def f(self, s):
print 's= %s'%s
k = KK()
k.f('Hello')
Is there a trick I need?
TIA
--
Time flies like the wind. Fruit flies like a banana. Stranger things have .0.
happened but none stranger than this. Does your driver's license say Organ ..0
Donor?Black holes are where God divided by zero. Listen to me! We are all- 000
individuals! What if this weren't a hypothetical question?
steveo at syslang.net
More information about the Python-list
mailing list