更改python中装饰参数的值

问题描述 投票:0回答:1

我有以下代码,其中默认的“用户”值为“无”,并且由装饰器“ 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
python python-3.x python-decorators
1个回答
0
投票

您可以使用字典:

© www.soinside.com 2019 - 2024. All rights reserved.