我是第一次使用装饰器,我创建了一个装饰器类来过滤目标函数的结果,它默认返回一个特定的序列:
class Filter(object):
def __init__(self, id=None):
self.id = id
def __call__(self, func):
def wrapper(*args):
entity_ids = func(*args)
result = {}
for k, v in entity_ids.items():
if self.id:
if '_' + str(self.id) in k:
result.update({k: v})
return result
return wrapper
我在其他一些类方法中使用装饰器,如下所示:
class SomeClass(object):
@Filter(id=None)
def get_ids(*args):
return result_sequence
调用类方法时如何定义装饰器的参数:
>>>sc = SomeClass()
>>>sc.get_ids(*args) # I want to pass the id kwarg for Filter here
提前致谢