this post was submitted on 12 Apr 2025
195 points (90.1% liked)

Technology

69156 readers
2972 users here now

This is a most excellent place for technology news and articles.


Our Rules


  1. Follow the lemmy.world rules.
  2. Only tech related news or articles.
  3. Be excellent to each other!
  4. Mod approved content bots can post up to 10 articles per day.
  5. Threads asking for personal tech support may be deleted.
  6. Politics threads may be removed.
  7. No memes allowed as posts, OK to post as comments.
  8. 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.
  9. Check for duplicates before posting, duplicates may be removed
  10. Accounts 7 days and younger will have their posts automatically removed.

Approved Bots


founded 2 years ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
[–] antlion@lemmy.dbzer0.com 7 points 1 week ago (2 children)

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.

[–] FooBarrington@lemmy.world 4 points 1 week ago (1 children)

This is honestly the worst version regarding readability. Don't rely on implicit coercion, people.

[–] antlion@lemmy.dbzer0.com 1 points 1 week ago (1 children)

But the first example does the same thing for an empty list. I guess the lesson is that if you’re measuring the speed of arbitrary stylistic syntax choices, maybe Python isn’t the best language for you.

[–] FooBarrington@lemmy.world 0 points 1 week ago* (last edited 1 week ago)

Yes, the first example does the same thing, but there's still less to mentally parse. Ideally you should just use if len(mylist) == 0:.

That's worse. IMO, solve this problem with two things:

  • type hint mylist as list | None or just list
  • 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.