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

Technology

69156 readers
3920 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 11 points 1 week ago* (last edited 1 week ago) (2 children)

isn't the expected behaviour exactly identical on any object that has len defined:

"By default, an object is considered true unless its class defines either a bool() method that returns False or a len() method that returns zero, when called with the object."

ps: well your objection is I guess that we cant know in advance if that said object has len defined such as being a collection so this question does not really apply to your post I guess.

[–] CompassRed@discuss.tchncs.de 16 points 1 week ago

It's not the same, and you kinda answered your own question with that quote. Consider what happens when an object defines both dunder bool and dunder len. It's possible for dunder len to return 0 while dunder bool returns True, in which case the falsy-ness of the instance would not depend at all on the value of len

[–] thebestaquaman@lemmy.world 5 points 1 week ago

Exactly as you said yourself: Checking falsieness does not guarantee that the object has a length. There is considerable overlap between the two, and if it turns out that this check is a performance bottleneck (which I have a hard time imagining) it can be appropriate to check for falsieness instead of zero length. But in that case, don't be surprised if you suddenly get an obscure bug because of some custom object not behaving the way you assumed it would.

I guess my primary point is that we should be checking for what we actually care about, because that makes intent clear and reduces the chance for obscure bugs.