Skip to main content

Command Palette

Search for a command to run...

Python's all docs are misleading

Updated
1 min read
Python's all docs are misleading

Going by the documentation for all, one would think all iterables will early return.

The following code will throw an AttributeError on the last item of the list iteratable.

def bad_all(o: object, att: str, l: list):
    if all([o is not None, getattr(o, att, None) in l]):
        return True
    return False

The subtlety here is since we’re using a list above, it will evaluate each item of the list first and not chain the logic as we expect to early return, resulting in the error.

Chain your logic explicitly using booleans to avoid this …

if o and getattr(o, att, None) in l:

… or use lazy generators which will early return.

Be sure to avoid some other python gotchas too.

References: