Python's all docs are misleading
Danny Crasto
1 min read
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:
0
Subscribe to my newsletter
Read articles from Danny Crasto directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Danny Crasto
Danny Crasto
I am developer/code-reviewer/debugger/bug-fixer/architect/teacher/builder from dubai, uae