简体   繁体   中英

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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