【发布时间】:2010-02-02 01:07:56
【问题描述】:
这是我的意思的一个例子:
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'... 那是什么?