Python:如何忽略函数中的参数?
我想知道是否有一种方法可以用参数定义函数,但如果函数中的某些参数不适用,请忽略它们 例如,在这段代码中,我试图从一个参考表中查找唯一保护伞下的联系人,以便向其发送电子邮件,但该表可能有一些行,其中联系人可能仅限于一到两个人,而不是五个人。如果是,则应忽略第一个/第二个联系人之后的所有其他联系人的参数Python:如何忽略函数中的参数?,python,function,arguments,ignore,win32com,Python,Function,Arguments,Ignore,Win32com,我想知道是否有一种方法可以用参数定义函数,但如果函数中的某些参数不适用,请忽略它们 例如,在这段代码中,我试图从一个参考表中查找唯一保护伞下的联系人,以便向其发送电子邮件,但该表可能有一些行,其中联系人可能仅限于一到两个人,而不是五个人。如果是,则应忽略第一个/第二个联系人之后的所有其他联系人的参数 reference = [ {'Code': '10', "Group": "There", "Contact": "Me@there.com", "Contact2": him@ther
reference = [
{'Code': '10', "Group": "There", "Contact": "Me@there.com",
"Contact2": him@there.com", Contact3": "you@there.com"},
{'Code': '11', "Group": "Here", "Contact": "she@here.com", "Contact2": "her@here.com"},
{'Code': '20', "Group": "Everywhere", "Contact": "them@everywhere.com"}
]
import win32com.client
def send_email(contact, contact2, contact3, contact4, contact5):
olMailItem = 0x0
obj = win32com.client.Dispatch("Outlook.Application")
newMail = obj.CreateItem(olMailItem)
newMail.Subject = "Email for %s" %group
newMail.Body = "Message"
newMail.To = contact
newMail.CC = contact2, contact3, contact 4, contact5
#newMail.BCC = "address"
attachment1 = file
newMail.Attachments.Add(attachment1)
#newMail.display()
newMail.Send()
count = 0
for Contact in reference:
send_email(reference['Contact'][count])
count = count + 1
可以使用数量可变的参数,但它们必须是函数的最后一个参数
def send_email(file, group, *contacts):
# ...
newMail.CC = ', '.join(contacts)
*
符号从您提供的结束参数创建一个元组
但是,在您的情况下,输入数据的结构简单,对您的应用程序来说很尴尬。您应该让它看起来更像这样:
reference = [
{'Code': '10', "Group": "There", "Contacts": ["Me@there.com", "him@there.com", "you@there.com"]},
{'Code': '11', "Group": "Here", "Contacts": ["she@here.com", "her@here.com"]},
{'Code': '20', "Group": "Everywhere", "Contacts": ["them@everywhere.com"]}
]
def send_email(contacts):
# ...
newMail.To = contacts[0]
newMail.CC = ', '.join(contacts[1:])
可以使用数量可变的参数,但它们必须是函数的最后一个参数
def send_email(file, group, *contacts):
# ...
newMail.CC = ', '.join(contacts)
*
符号从您提供的结束参数创建一个元组
但是,在您的情况下,输入数据的结构简单,对您的应用程序来说很尴尬。您应该让它看起来更像这样:
reference = [
{'Code': '10', "Group": "There", "Contacts": ["Me@there.com", "him@there.com", "you@there.com"]},
{'Code': '11', "Group": "Here", "Contacts": ["she@here.com", "her@here.com"]},
{'Code': '20', "Group": "Everywhere", "Contacts": ["them@everywhere.com"]}
]
def send_email(contacts):
# ...
newMail.To = contacts[0]
newMail.CC = ', '.join(contacts[1:])
您可以使用以下内容:
def myFunction(*arg):
这允许您有一个可变数量的参数….您可以使用以下内容:
def myFunction(*arg):
这允许你有一个可变数量的参数….我肯定是想得太多了。我最终做了以下几点:
import win32com.client
table = [
{'Code': '10', "Group": "Turn", "Contact": "A@there.com; B@there.com"},
{'Code': '20', "Group": "A9", "Contact": "Z@here.com; Y@here.com; H@here.com"},
{'Code': '30', "Group": "AppNexus", "Contact": "N@OverThere.com"}
]
def send_email(group, file, contact):
olMailItem = 0x0
obj = win32com.client.Dispatch("Outlook.Application")
newMail = obj.CreateItem(olMailItem)
newMail.Subject = "Email for %s" %group
newMail.Body = "Message"
newMail.To = contact
#newMail.CC =
#newMail.BCC = "address"
attachment1 = file
newMail.Attachments.Add(attachment1)
#newMail.display()
newMail.Send()
count = 0
for Contact in table:
info = get_info(table['Code'][count])
file = make_file(table['Code'][count], info)
send_email(file, table['Code'][count], table['Group'], table['Contact'][count])
count = count + 1
字典值只需要分号和引号就可以将所有电子邮件的总数括起来
电话,
newMail.To =
只需要取一个字符串值,Outlook在实际Outlook应用程序中使用分号作为分隔符。因此,您只需传递包含多封电子邮件的条目,而无需将其放入字典中的单独列表,只需执行并打开quote,列出所有电子邮件,然后关闭该特定组联系人条目中最后一封电子邮件的quote。此表中的大多数电子邮件不会更改,因此也不必担心这一点。我肯定是想得太多了。我最终做了以下几点:
import win32com.client
table = [
{'Code': '10', "Group": "Turn", "Contact": "A@there.com; B@there.com"},
{'Code': '20', "Group": "A9", "Contact": "Z@here.com; Y@here.com; H@here.com"},
{'Code': '30', "Group": "AppNexus", "Contact": "N@OverThere.com"}
]
def send_email(group, file, contact):
olMailItem = 0x0
obj = win32com.client.Dispatch("Outlook.Application")
newMail = obj.CreateItem(olMailItem)
newMail.Subject = "Email for %s" %group
newMail.Body = "Message"
newMail.To = contact
#newMail.CC =
#newMail.BCC = "address"
attachment1 = file
newMail.Attachments.Add(attachment1)
#newMail.display()
newMail.Send()
count = 0
for Contact in table:
info = get_info(table['Code'][count])
file = make_file(table['Code'][count], info)
send_email(file, table['Code'][count], table['Group'], table['Contact'][count])
count = count + 1
字典值只需要分号和引号就可以将所有电子邮件的总数括起来
电话,
newMail.To =
只需要取一个字符串值,Outlook在实际Outlook应用程序中使用分号作为分隔符。因此,您只需传递包含多封电子邮件的条目,而无需将其放入字典中的单独列表,只需执行并打开quote,列出所有电子邮件,然后关闭该特定组联系人条目中最后一封电子邮件的quote。此表中的大多数电子邮件不会更改,因此也无需担心。只需不使用参数即可。没有任何法律反对…只是。。。忽略它?你不必做任何特别的事情来忽略事情。我觉得我就像是在一个金融咨询节目上,回答关于如何不买东西的问题。我认为问题是应该通过什么,在这种情况下,也许你想要默认的论点,或者是可变长度的声明。@Jean-Françoisfare我很想给一位国会议员写一封关于这一点的笑话电子邮件,但了解美国政治,这将是唯一可以通过的法律,我们会被卡住。只是不要使用参数。没有任何法律反对…只是。。。忽略它?你不必做任何特别的事情来忽略事情。我觉得我就像是在一个金融咨询节目上,回答关于如何不买东西的问题。我认为问题是应该通过什么,在这种情况下,也许你想要默认的论点,或者是可变长度的咒语。@Jean-Françoisfare我很想给一位国会议员写一封关于这一点的笑话电子邮件,但了解美国政治,这将是唯一需要通过的法律,我们会被卡住。我添加了一个引用表,我使用循环语句来迭代它。这就是我现在正在做的…@Korean_Yeezus你的问题有点难理解,但我想我现在已经知道了。看一下更新。我添加了一个引用表,我使用循环语句对它进行迭代。这就是我现在正在做的…@Korean_Yeezus你的问题有点难理解,但我想我现在已经知道了。看看更新。