如果我要创建一个函数:
def function(x, y):
"""A simple function, but 'y' will be a tuple.
"""
function = (a*b) + x
我希望y
成为(a,b),所以当打印出来时,我必须这样做:
print function(3,(4, 5))
=23
其中x是3,a是4,b是5.我明白我可以做到这一点,而不必将参数变成元组,但它是我给予的一项任务,我必须这样做。
答案 0 :(得分:3)
将using System.Net.Mail;
...
MailMessage mail = new MailMessage("you@yourcompany.com", "user@hotmail.com");
SmtpClient client = new SmtpClient();
client.Port = 25;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Host = "smtp.google.com";
mail.Subject = "this is a test email.";
mail.Body = "this is my test email body";
client.Send(mail);
解包到y
和a
b
答案 1 :(得分:1)
通过使用print语句,我们可以看到你正在使用Python2,所以你可以像这样在函数声明中解包元组
def function(x, (a, b)):
"""A simple function
"""
return (a * b) + x
print function(3,(4, 5))
Python3中不再允许使用此语法,但是通过" 2to3"运行脚本。产量
def function(x, xxx_todo_changeme):
"""A simple function
"""
(a, b) = xxx_todo_changeme
return (a * b) + x
答案 2 :(得分:1)
您也可以使用operator
lib并使用*
解压缩:
from operator import mul
def function(x, y):
"""A simple function, but 'y' will be a tuple.
"""
return x + mul(*y)
print(function(1,(3,4)))
将输出
13