3
请考虑以下简单的例子:Python的装饰与参数只调用一次
permitted = True
class is_allowed(object):
def __init__(self, some_arg):
# this is actually needed in the complete code
self.some_arg = some_arg
def __call__(self, f):
if permitted == False:
raise Exception("not authenticated to do that")
def wrapped_f(*args, **kwargs):
f(*args, **kwargs)
return wrapped_f
@is_allowed("blah")
def print_hi():
print("hi")
print_hi()
permitted = False
print_hi()
我想这个问题是该函数print_hi()定义为当装饰只调用一次。因为全局变量的变化没有效果。有什么办法可以避免这种行为?
很酷,谢谢。接受在10分钟;) – Stefan
为OP的好处,你应该真的这样做:'如果允许== False'像这样:'如果不允许' – Anentropic
@Antropic yes一开始没有接受,我已经更新 –