我想检查Python函数是否经过修饰,并将decorator参数存储在函数dict中。这是我的代码:
from functools import wraps
def applies_to(segment="all"):
def applies(func):
@wraps(func)
def wrapper(*args, **kwargs):
func.func_dict["segment"] = segment
print func.__name__
print func.func_dict
return func(*args, **kwargs)
return wrapper
return applies
@applies_to(segment="mysegment")
def foo():
print "Some function"
> foo() # --> Ok, I get the expected result
foo
{'segment': 'mysegment'}
> foo.__dict__ # --> Here I get empty result. Why is the dict empty?
{}