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.