this post was submitted on 24 Apr 2025
46 points (92.6% liked)
Python
7041 readers
106 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
I prefer the clarity of
len(x) == 0
personally; it's a pretty low syntactic penalty to clarify intent.Obviously, if your variable names are good and you're consistent about your usage
not list
can be fine too, but it is a very JavaScript-y thing to me to be that vague and/or rely on "truthiness."The notion of truthiness is defined by the language.
It's not something that happens to work, it's literally defined that way.
if not x
is the common way to tell if you have data or not, and in most cases, the difference betweenNone
and empty data ([]
,{}
, etc) isn't important.len(x) == 0
will raise an exception if you give itNone
, and that's usually not what you want. So I guess the verbose way to do that isif x is None or len(x) == 0:
, but that's exactly equivalent toif not x
, with the small caveat that it doesn't check if the value has__len__
implemented. If you're getting wonky types thrown around (i.e. getting a class instance when expecting a list), you have bigger problems.I use type hinting on pretty much all "public" methods and functions, and many of my "private" methods and functions as well. As such, sending the wrong types is incredibly unlikely, so
not x
is more than sufficient and clearly indicates intent (do I have data?).I did not say it's not semantically well defined.
https://en.wikipedia.org/wiki/Brainfuck#Hello_World! -- this is semantically well defined, but it's still vague. Vagueness is a property of how well the syntax is conveying intent.
It's only vague if coming from a language where it's invalid or vague semantically. For example:
[]
is truthy for whatever reasonint x[] = {};
evaluates to true because it's a pointer; C only evaluates to false if something is 0nil
orfalse
The only surprising one here is Javascript. I argue Lua and Python make sense for the same reason, Lua just decided to evaluate truthiness based on whether the variable is set, whereas Python decided to evaluate it based on the contents of the variable. I prefer the Python approach here, but I prefer Lua as a language generally (love the simplicity).