jeudi 22 mars 2018

Python - Check if function returns value and capture it if so

At first, this question may seem rather stupid. But for some reason, I cannot figure it out for the life of me. I have some function, lets call it foo. foo will either return False or a unique string / number depending on what parameters are passed to it. I want to check if foo returns some value other then False, and if so process it. If it doesn't, I want to go on and do the same thing with a different parameter passed to foo. It can be done as below:

a = foo(1)
b = foo(2)
c = foo(3)
if a:
    print (a)
elif b:
    print (b + 3)
elif c:
    print ("abc" + c)

While this method works, it seems "clunky" to me. Is there any better way of doing this?

Aucun commentaire:

Enregistrer un commentaire