繁体   English   中英

在这种情况下,如何在不声明全局变量的情况下将一个函数的变量用于另一个函数?

[英]How to use variable of one function to another function without declaring global variable in this case?

我有一个这样的功能:

def upload_file(request):
    if request.method == 'POST':    
        # session_name = request.POST['session']

        url = request.POST['hostname']

        username = request.POST['username']

        password = request.POST['password']

此函数从表单获取主机名,用户名和密码,并将其存储在相应的变量中。 我想在另一个函数中使用变量url,用户名和密码:

def another_function():
    print url, username, password

什么是正确的方法? 我可以使upload_file返回该变量并使用它,但我想只能用一个变量来完成。

好了,您可以在函数的末尾添加:

    return url, username, password

然后做

    url, username, password=upload_file(request)  

这样,第一个函数将返回所有三个变量,然后您可以在另一个函数中使用它们!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM