我有以下代码,其中默认的“用户”值为“无”,并且由装饰器“ need_authentication”获取,这种情况是我更改了用户值,但是当我调用装饰器时,它总是得到“无”。我现在User变量不是“ None”,因为我在调用show_content之前将其打印出来这是我的代码:
import user
User = None
def login():
#In this function, I change User value
@user.need_authentication(actual_user=User)
def show_content():
print('contenido')
login()
show_content():
print("content")
这是我的装饰:
def need_authentication(actual_user = None):
def decorator(func):
def check_authentication(*args, **kwargs):
print(app.User)
print(user)
if user == None:
print("lo siento, necesita registrarse para acceder al contenido")
else:
func(*args, **kwargs)
return check_authentication
return decorator
您可以使用字典: