[英]Change value of decorator parameter variable in python
我有以下代碼,其中默認的“用戶”值為 None 並且它由裝飾器“need_authentication”獲取,情況是我更改了用戶值,但是當我調用裝飾器時,它總是得到 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
你可以使用字典:
actual_user = {'user': None}
def need_authentication(actual_user = actual_user):
def decorator(func):
def check_authentication(*args, **kwargs):
print(app.User)
user = actual_user[user]
print(user)
if user == None:
print("lo siento, necesita registrarse para acceder al contenido")
else:
func(*args, **kwargs)
return check_authentication
return decorator
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.