vendredi 15 décembre 2017

python: Piping a value through an if/elif statement

I have a function that can return one of three values: a, b, or c.

def f():
    return x # a, b, or c

I need to execute different statements based on the return value of f(): A, B, or C. The best way to do this, that I know of, is:

v = f()
if v == a:
    # Do A
elif v == b:
    # Do B
else:
    # Do C

Without introducing this new variable v, is there a different way to evaluate the return value of f()?
Something like this, maybe?

if f() == a:
    # Do A
elif _value_from_above == b:
    # Do B
else:
    # Do C

Aucun commentaire:

Enregistrer un commentaire