this post was submitted on 24 Apr 2025
47 points (92.7% liked)

Python

7042 readers
108 users here now

Welcome to the Python community on the programming.dev Lemmy instance!

πŸ“… Events

PastNovember 2023

October 2023

July 2023

August 2023

September 2023

🐍 Python project:
πŸ’“ Python Community:
✨ Python Ecosystem:
🌌 Fediverse
Communities
Projects
Feeds

founded 2 years ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
[–] Kissaki 13 points 14 hours ago* (last edited 14 hours ago) (3 children)

One of the principles of the Pythonic style is: simple is better than complex.

But is it when you consider ambiguity and expressiveness too?

len(mylist) tells me it's definitely a list and not null or whatever. It also tells me the intent of whoever writes the code was checking length.

If it requires a long article to explain, I'd certainly be hesitant to use and prefer. The question is, is it surprising or intuitive that the truthy becomes a length check. What do you want to express with your code. And who will read it, and what expectations and requirements do you want to require of them.

For sequence type objects, such as lists, their truth value is False if they are empty.

[–] Michal 3 points 8 hours ago

It's just how pythonic code is written. All python developers know that not and bool can indicate that variable is either empty or null. They will also use the in operator and other python specific syntax.

[–] [email protected] 5 points 11 hours ago* (last edited 11 hours ago)

len(mylist) tells me it’s definitely a list and not null or whatever

Do you know what else can tell you it's a list? Type hinting.

If I care about the distinction between None and an empty list, I'll make separate checks. len(None) raises an exception, and most of the time that's not what I want when checking whether there's something in the list, so not x is generally preferred, especially when type hinting is used consistently enough to be reasonably sure I'm actually getting a list or None. If I get something else, that means things got really broken and they'll likely get an exception alter (i.e. when doing anything list-like).

For sequence type objects, such as lists, their truth value is False if they are empty.

That's generally exactly what I want. len(x) == 0 doesn't tell you it's a list, it just tells you it has __len__. So that could be a dict, list, or a number of other types that have a length, but aren't lists.

Don't rely on checks like that to tell you what type a thing is.

[–] [email protected] 3 points 9 hours ago (1 children)

foo = "potatoes!"

len(foo)

will give you the number of characters in the string. It's not a surefire way to find a list at all!

[–] [email protected] 2 points 6 hours ago

Same with dictionaries, iterators (will consume the iterator!), and anything else that has a length.

I've actually had the string instead of list issue a number of times, because sometimes things get off and it's hard to track down where things went wrong.

For this reason, I think type hints are the way to go. Use them consistently w/ a static analysis tool like pyright and you'll catch a lot of these issues before running into cryptic error messages.