![](/img/trans.png)
[英]How do I use the same variable in one function with another function, without making that variable Global?
[英]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.