在Python中调用一个函数的变量到另一个函数

时间:2013-12-25 03:48:51

标签: python

我正在编写一个程序来通过python发送电子邮件。我有不同的def,包含包含电子邮件用户名和电子邮件密码等的变量。然后我有另一个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

1 个答案:

答案 0 :(得分:10)

您需要在辅助函数中返回值,并从主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