繁体   English   中英

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

[英]Calling a variable from one function to another function in 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

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

暂无
暂无

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

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