在函数和类之外调用user_name变量

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

我想在函数和类之外访问user_name变量,该怎么做?

这是我的代码

'''

 def DocumentCreateView(request):

user_name = request.user.email

        print(user_name)
        int_rep = int(rep_no)
        flag = 1
        count = 0
        i = 1
        j = 0

'''

这是我要在类和函数之外访问用户名的代码

path_to_piepline1 = "lazy-test/users/"+**user_name**+"/input"
print("--- first argument ---", path_to_piepline1)

plz忽略身份

python python-3.x python-2.7
2个回答
1
投票

您应该返回

def DocumentCreateView(request):

    user_name = request.user.email

    print(user_name)
    int_rep = int(rep_no)
    flag = 1
    count = 0
    i = 1
    j = 0

    return user_name

然后调用此函数:

user_name = DocumentCreateView(<pass your argument here>)
path_to_piepline1 = "lazy-test/users/"+ user_name +"/input"

但是,如果您不以任何形式(单独或与其他数据一起)返回它,并且它保持本地状态,则在函数返回后将无法访问它。


0
投票

创建类的对象,并使用它来调用函数并返回用户名的值。

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