by robot_no_421 · 3y ago · 0 comments
if my_container.empty():
# do stuff
than if not my_container:
# do stuff
The second implementation behaves the same if either my_container is None or if my_container is empty (or if my_container is 0 or ""...), whereas the first implementation allows me to handle those cases differently. The first implementation is also more explicit and easier to understand on a first read.It's also easier to debug. Is users supposed to be None in your example? Possibly, but your code doesn't give me any hints. Is my_container supposed to be None in my example? Probably not based on the way I explicitly used the .empty() method.
Really, though, list.empty() is canonically spelled “not list” in Python. That’s the idiom to learn, read, and write.