this post was submitted on 12 Apr 2025
197 points (90.5% liked)

Technology

69298 readers
3872 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
[–] iAvicenna@lemmy.world 28 points 2 weeks ago* (last edited 2 weeks ago) (42 children)

Yea and then you use "not" with a variable name that does not make it obvious that it is a list and another person who reads the code thinks it is a bool. Hell a couple of months later you yourself wont even understand that it is a list. Moreover "not" will not throw an error if you don't use an sequence/collection there as you should but len will.

You should not sacrifice code readability and safety for over optimization, this is phyton after all I don't think list lengths will be your bottle neck.

[–] Artyom@lemm.ee 1 points 1 week ago (2 children)

In my experience, if you didn't write the function that creates the list, there's a solid chance it could be None too, and if you try to check the length of None, you get an error. This is also why returning None when a function fails is bad practice IMO, but that doesn't seem to stop my coworkers.

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

good point I try to initialize None collections to empty collections in the beginning but not always guaranteed and len would catch it

Sometimes there's an important difference between None and []. That's by far not the most common use, but it does exist (e.g. None could mean "user didn't supply any data" and [] could mean "user explicitly supplied empty data").

If the distinction matters, make it explicit:

if foo is None:
    raise ValueError("foo must be defined for this operation")
if not foo:
    return None

for bar in foo:
    ...

return some_other_value

This way you're explicit about what constitutes an error vs no data, and the caller can differentiate as well. In most cases though, you don't need that first check, if not foo can probably just return None or use some default value or whatever, and whether it's None or [] doesn't matter.

if len(foo) == 0: is bad for a few reasons:

  • TypeError will be raised if it's None, which is probably unexpected
  • it's slower
  • it's longer

If you don't care about the distinction, handle both the same way. If you do care, handle them separately.

[–] LegoBrickOnFire@lemmy.world 1 points 1 week ago

Passing None to a function expecting a list is the error...

load more comments (39 replies)