Pythonic way to avoid “if x: return x” statements

Alternatively to Martijn’s fine answer, you could chain or. This will return the first truthy value, or None if there’s no truthy value:

def check_all_conditions():
    return check_size() or check_color() or check_tone() or check_flavor() or None

Demo:

>>> x = [] or 0 or {} or -1 or None
>>> x
-1
>>> x = [] or 0 or {} or '' or None
>>> x is None
True

Leave a Comment