mercredi 8 mars 2017

Oneliner to replace if-else block [duplicate]

This question already has an answer here:

I want to pass one particular keyword argument from function to another, but only if it exists, like below. I think however that that if-else-statement is a bit too long for what it is doing: extracting one item from a dict if it exists. Is there a shorter way to write this if-else-block?

def f(**kwargs):
    if 'a' in kwargs:
        kwargs = {'a': kwargs['a']}
    else:
        kwargs = {}
    g(kwargs)

def g(**kwargs):
    print(kwargs)

Aucun commentaire:

Enregistrer un commentaire