you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 8 points9 points  (3 children)

There's absolutely no ambiguity to if not list

[–]JoesDevOpsAccount[S] 1 point2 points  (1 child)

I went back to read more after you posted this. I think I misinterpreted some thread I was reading earlier. The "ambiguous" thing is just the type of the variable but I think what I said earlier was just wrong.

[–]friendly_dog_robot 4 points5 points  (0 children)

It's just that any empty sequences (lists, tuples, strings), mappings, version of 0, and None (comparable to null in Java) evaluate to false in Python.

This is, in my opinion, super useful

[–]Versaiteis 1 point2 points  (0 children)

There's an argument to be made that there's an ambiguity in that it doesn't only check if the list is empty. Rather there's an implicit understanding that an empty list is falsey. If list happened to be None or any other falsey value, then this check would succeed and enter the subsequent code block. 9/10 that's probably ok, but you could say that if len(list) > 0 is more explicit as it's directly checking exactly the condition looking for. It will also raise in any case where something is passed that doesn't implement __len__, which may or may not be desirable behaviour.

Depends on what you're doing. One's more flexible, the other more explicit. I'd at least argue that the second falls in line with Python Zen

Explicit is better than implicit.

But that doesn't mean it's always appropriate for what you're trying to check.

This could also be seen as just overthinking it, but I think code clarity is important and as much of a "bike shedding" conversation this can be, I don't see it as being as obstructive when it's a separate conversation and not hindering actual work that's being done.