這裏是我的意思的例子:Python:如何從類裝飾器中訪問裝飾類的實例?
class MyDecorator(object):
def __call__(self, func):
# At which point would I be able to access the decorated method's parent class's instance?
# In the below example, I would want to access from here: myinstance
def wrapper(*args, **kwargs):
return func(*args, **kwargs)
return wrapper
class SomeClass(object):
##self.name = 'John' #error here
name="John"
@MyDecorator()
def nameprinter(self):
print(self.name)
myinstance = SomeClass()
myinstance.nameprinter()
我需要裝飾實際的類?
'self.name ='John'' ...那是什麼? – jldupont 2010-02-02 01:22:16