【发布时间】:2020-02-06 14:26:47
【问题描述】:
我想在我的烧瓶应用程序中记录 MongoEngine 文档,如 here 所述。但我也希望能够排除此文档类具有的某些属性。
如果我给装饰器的应用函数一个附加参数excludes,则不再给出 cls 参数。
Traceback (most recent call last):
File ".../test.py", line 26, in <module>
@__log_update.apply(excludes=[])
TypeError: apply() missing 1 required positional argument: 'cls'
在简化代码中
def handler(*events):
"""
Signal decorator to allow use of callback functions as class decorators.
"""
def decorator(fn):
def apply(cls, excludes=[]):
for exclude in excludes:
print(exclude)
for event in events:
event.connect(fn, sender=cls)
return cls
fn.apply = apply
return fn
return decorator
@handler()
def __log_update(*args, **kwargs):
pass
@__log_update.apply(excludes=['testString'])
class Test(object):
testString = ''
testInt = 0
但是当只使用@__log_update.apply 没有任何参数时,cls 参数被给出并且应该是<class '__main__.Test'>。
我需要两者都做日志记录。
【问题讨论】: