3
는 다음과 같은 간단한 예를 고려하시기 바랍니다 일단라는 인수 파이썬 데코레이터 : 나는 문제가 함수 print_hi()가 정의 될 때 데코레이터는 한 번만 호출되는 것 같다만
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()
. 이 때문에 전역 변수의 변경은 아무 효과가 없습니다. 이 행동을 포기할 방법이 있습니까?
멋진 답변입니다. 10 분 안에 받아 들일 수 있습니다.) – Stefan
OP의 이익을 위해, 당신은 정말로 이것을해야합니다 :'허용 된 경우 == 거짓'같은 경우 :'허용되지 않을 경우' – Anentropic
@Anentropic 예 처음에 그걸 알아 채지 못했습니다. –