【发布时间】:2019-03-17 22:23:20
【问题描述】:
我正在学习使用此资源的装饰器类:
http://book.pythontips.com/en/latest/decorators.html#decorator-classes
呈现的类基本上是这样的:
class logit(object):
def __init__(self, logfile='out.log'):
self.logfile = logfile
def __call__(self, func):
log_string = func.__name__ + " was called"
print(log_string)
# Open the logfile and append
with open(self.logfile, 'a') as opened_file:
# Now we log to the specified logfile
opened_file.write(log_string + '\n')
# Now, send a notification
self.notify()
def notify(self):
# logit only logs, no more
pass
和电话:
@logit()
def myfunc1():
pass
myfunc1()
我收到一个错误:
>>> myfunc1()
[...]
TypeError: 'NoneType' object is not callable
【问题讨论】:
-
鉴于您的调用,确保您的
__call__方法返回一个可调用的(可能是return func?)
标签: python class decorator python-decorators