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
Past
November 2023
- PyCon Ireland 2023, 11-12th
- PyData Tel Aviv 2023 14th
October 2023
- PyConES Canarias 2023, 6-8th
- DjangoCon US 2023, 16-20th (!django π¬)
July 2023
- PyDelhi Meetup, 2nd
- PyCon Israel, 4-5th
- DFW Pythoneers, 6th
- Django Girls Abraka, 6-7th
- SciPy 2023 10-16th, Austin
- IndyPy, 11th
- Leipzig Python User Group, 11th
- Austin Python, 12th
- EuroPython 2023, 17-23rd
- Austin Python: Evening of Coding, 18th
- PyHEP.dev 2023 - "Python in HEP" Developer's Workshop, 25th
August 2023
- PyLadies Dublin, 15th
- EuroSciPy 2023, 14-18th
September 2023
- PyData Amsterdam, 14-16th
- PyCon UK, 22nd - 25th
π Python project:
- Python
- Documentation
- News & Blog
- Python Planet blog aggregator
π Python Community:
- #python IRC for general questions
- #python-dev IRC for CPython developers
- PySlackers Slack channel
- Python Discord server
- Python Weekly newsletters
- Mailing lists
- Forum
β¨ Python Ecosystem:
π Fediverse
Communities
- #python on Mastodon
- c/django on programming.dev
- c/pythorhead on lemmy.dbzer0.com
Projects
- PythΓΆrhead: a Python library for interacting with Lemmy
- Plemmy: a Python package for accessing the Lemmy API
- pylemmy pylemmy enables simple access to Lemmy's API with Python
- mastodon.py, a Python wrapper for the Mastodon API
Feeds
founded 2 years ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
view the rest of the comments
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.
It's just how pythonic code is written. All python developers know that
not
andbool
can indicate that variable is either empty or null. They will also use thein
operator and other python specific syntax.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, sonot x
is generally preferred, especially when type hinting is used consistently enough to be reasonably sure I'm actually getting a list orNone
. 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).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 adict
,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.
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!
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.