[英]Python: How can you ignore an argument in a function?
I am wondering if there is a way to define a function with arguments, but ignore some arguments within the function if they are not applicable. 我想知道是否有一种方法来定义带有参数的函数,但是如果它们不适用,则忽略函数中的某些参数。
For instance, in this code, I am trying to find contacts under a unique umbrella from a reference table to send an email to, but the table may have rows where contacts may be limited to maybe just one or two people vs five. 例如,在这段代码中,我试图从参考表中查找唯一的伞下的联系人以向其发送电子邮件,但是该表可能包含行,其中的联系人可能仅限于一两个人或五个人。 If so, the argument for all other contacts following the first/second one should be ignored. 如果是这样,则应忽略第一个/第二个联系人之后的所有其他联系人的参数。
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
You can use a variable number of arguments, but they must be the last arguments to the function. 您可以使用可变数量的参数,但是它们必须是函数的最后一个参数。
def send_email(file, group, *contacts):
# ...
newMail.CC = ', '.join(contacts)
The *
notation creates a tuple from the ending arguments however many you provide. *
符号会根据结尾的参数创建一个元组,无论您提供了多少。
In your case though, the input data is simply structured in a way that's awkward for your application. 但是,在您的情况下,输入数据的结构只是对您的应用程序来说很尴尬。 You should make it look more like this: 您应该使它看起来像这样:
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:])
you can use something like: 您可以使用类似:
def myFunction(*arg):
this allow you to have a variable number of argument.... 这使您可以使用可变数量的参数。
I was definitely overthinking this. 我绝对是在想这个。 What I ultimately did is below: 我最终要做的是:
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
The dictionary values just needed semicolons and to have the quotes wrap around the total of all the emails. 字典值仅需使用分号,并使用引号将所有电子邮件的总和包裹起来。
The call, 电话,
newMail.To =
Just needs to take one string value and Outlook takes a semicolon as a separator within the actual Outlook application. 只需使用一个字符串值,Outlook在实际的Outlook应用程序中将分号作为分隔符。 Therefore you can just pass on an entry with multiple emails without making them into a separate list within the dictionary by just doing and open quote, listing all the emails, and closing the quote on the last email within the contact entry for that particular group. 因此,您只需传递并打开引号,列出所有电子邮件,并关闭该特定组的联系人条目中最后一封电子邮件的引号,就可以传递包含多封电子邮件的条目,而不必将它们放入词典中的单独列表中。 Most emails within this table would not change, so there is no need to worry about that either. 该表中的大多数电子邮件都不会更改,因此也无需担心。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.