如何调用函数并返回变量

时间:2017-06-06 10:51:26

标签: python python-2.7 function

请有人帮我理解如何用Python编写初学者函数吗?

我想要做的具体事情是......

我有2个变量,我想对它们两个应用相同的功能,最后有2个新东西叫做不同的东西,我最好怎么做?

alen = 3
blen = 3

如何根据变量?

调用foo以明确命名的输出结束
message = MIMEMultipart()
message['Subject'] = "Attachment Test"
message['From'] = 'myemail'
message['Reply-to'] = 'myemail'
message['To'] = 'otheremail'

text = MIMEText("Message Body")
message.attach(text)

directory = "C:\ExamplePDF.pdf"
with open(directory, encoding = 'utf-8', errors = 'replace') as opened:
    openedfile = opened.read()
attachedfile = MIMEApplication(openedfile, _subtype = "pdf", _encoder = encode_base64)
attachedfile.add_header('content-disposition', 'attachment', filename = "ExamplePDF.pdf")
message.attach(attachedfile)

server = SMTP("smtp.gmail.com:587")
server.ehlo()
server.starttls()
server.login("myemail", "password")
server.sendmail(message['From'], message['To'], message.as_string())
server.quit()

1 个答案:

答案 0 :(得分:0)

您的函数需要实际返回一个值。请记住,return不是您调用的函数(如return()),而是一个语句,后跟您要返回的内容。因此,在您的情况下,您的函数应以return thing结束。

如何实际使用该功能很简单:

a = "abc"
alen = foo(a)