I haven't read the article. But I'd assume this is for the same reason that not not string
is faster than bool(string)
. Which is to say that it has to do with having to look up a global function rather than a known keyword.
this post was submitted on 12 Apr 2025
194 points (90.1% liked)
Technology
68772 readers
4722 users here now
This is a most excellent place for technology news and articles.
Our Rules
- Follow the lemmy.world rules.
- Only tech related news or articles.
- Be excellent to each other!
- Mod approved content bots can post up to 10 articles per day.
- Threads asking for personal tech support may be deleted.
- Politics threads may be removed.
- No memes allowed as posts, OK to post as comments.
- Only approved bots from the list below, this includes using AI responses and summaries. To ask if your bot can be added please contact a mod.
- Check for duplicates before posting, duplicates may be removed
- Accounts 7 days and younger will have their posts automatically removed.
Approved Bots
founded 2 years ago
MODERATORS
194
Python Performance: Why 'if not list' is 2x Faster Than Using len()
(blog.codingconfessions.com)
Could also compare against:
if not len(mylist)
That way this version isn’t evaluating two functions. The bool evaluation of an integer is false when zero, otherwise true.
That's worse. IMO, solve this problem with two things:
- type hint
mylist
aslist | None
or justlist
- use
if not mylist:
The first documents intent and gives you static analysis tools some context to check for type consistency/compatibility, and the second shows that None
vs empty isn't an important distinction here.
This is honestly the worst version regarding readability. Don't rely on implicit coercion, people.
load more comments
(2 replies)