Python decorator question
Posted
by nsharish
on Stack Overflow
See other posts from Stack Overflow
or by nsharish
Published on 2010-05-09T08:30:40Z
Indexed on
2010/05/09
8:48 UTC
Read the original article
Hit count: 266
decorator 1:
def dec(f):
def wrap(obj, *args, **kwargs):
f(obj, *args,**kwargs)
return wrap
decorator 2:
class dec:
def __init__(self, f):
self.f = f
def __call__(self, obj, *args, **kwargs):
self.f(obj, *args, **kwargs)
A sample class,
class Test:
@dec
def disp(self, *args, **kwargs):
print(*args,**kwargs)
The follwing code works with decorator 1 but not with decorator 2.
a = Test()
a.disp("Message")
I dont understand why decorator 2 is not working here. Can someone help me with this?
© Stack Overflow or respective owner