在Python中从一个函数调用变量到另一个函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20768856/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Calling a variable from one function to another function in Python
提问by micma
I'm writing a program to send an email through python. I have different def's that hold the variables that contain the email username and email password etc.. Then I have another def that actual sends the email, but it needs the variable that contains the email username and password etc.. How can I call the variables from the different def's? Sorry for the weird wording. I don't know how else to say it :P Thanks!
我正在编写一个程序来通过 python 发送电子邮件。我有不同的 def 保存包含电子邮件用户名和电子邮件密码等的变量。然后我有另一个 def 实际发送电子邮件,但它需要包含电子邮件用户名和密码等的变量。我如何调用来自不同定义的变量?对不起,奇怪的措辞。我不知道还能怎么说 :P 谢谢!
def get_email_address():
#the code to open up a window that gets the email address
Email = input
def get_email_username():
#the code to open up a window that gets email username
Email_Username = input
#same for the email recipient and email password
def send_email():
#here I need to pull all the variables from the other def's
#code to send email
采纳答案by Holy Mackerel
You would need to return values in your helper functions and call those functions from your main send_email()function, assigning the returned values to variables. Something like this:
您需要在辅助函数中返回值并从主send_email()函数调用这些函数,将返回值分配给变量。像这样的东西:
def get_email_address():
#the code to open up a window that gets the email address
Email = input
return Email
def get_email_username():
#the code to open up a window that gets email username
Email_Username = input
return Email_Username
#same for the email recipient and email password
def send_email():
# variables from the other def's
email_address = get_email_address()
email_username = get_email_username()
#code to send email

