简体   繁体   中英

Best pythonic way of checking if a parameter is passed in a function or not? Is inline checking possible?

I'm making a simple function to send mails with mutt. Sometimes I'll need to send attachments and sometimes not, so I need to check if the parameter "attachment" have something on it. Right now my code looks like this:

def sendMail(destino,asunto,cuerpo,adjunto):
    try:
        os.system('echo "' + cuerpo + '" | mutt -s "' + asunto + '" ' + destino)

How whould be the proper way of checking if "adjunto" (attachment variable) contains something and add "-a adjunto" to the command only if there's an attachment? I know I could do a regular "if" statement and use a different os.system if I have some attachment, but I want to know if there's any way of doing that check inline. Something like "..... asunto + '" ' + destino (('+ ' + adjunto) if adjunto=true)"

PS: I know the code is still not finished, but I want to know how to efficiently check for attachments.

You can use default value for adjunto, you do it when defining the function like so:

def sendMail(destino, asunto, cuerpo, adjunto=None):
    try:
        os.system('echo "' + cuerpo + '" | mutt -s "' + asunto + '" ' + destino)

You can also use the language hack with or command:

adjunto=None
print(adjunto or 'a')

Output:
    a

It will add nothing to the string your injecting.

Btw you should use ''. format function, it's far more pythonic.

os.system('echo "{}" | mutt -s "{}" {}'.format(cuerpo, asunto, destino)

You can do it this way:

destino + ((" " + adjunto) if adjunto else "")

but you probably shouldn't, unless you are very sure that the name of the attachment really is a filename and not some malicious shell command. And consider using the subprocess module instead of os.system .

You can use a one-line if statement in Python: output if condition else other_output

It would look like this for your example:

'-a {}'.format(adjunto) if adjunto != None else ''

So you code could be:

    adjunto = '-a {}'.format(adjunto) if adjunto != None else ''
    os.system('echo "' + cuerpo + '" | mutt -s "' + adjunto + '" ' + destino)
def sendMail(destino,asunto,cuerpo,adjunto = None):
    adjunto_mandato = ' -a ' + adjunto if adjunto is not None else ''
    mandato = 'echo "' + cuerpo + '" | mutt -s "' + asunto + '" ' + destino + adjunto_mandato

    try:
        os.system(mandato)
    except Exception as e:
        print(e)

This is the most stimple "Inline" way that I can think of to accomplish what you are asking. I supose you need to balance being able to read your code and have it inline because if you drop the Try block which you shouldn't do you could do the system call in a ternary operator:

os.system( ) if adjunto is not None else os.system( )

buenos suerte!

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