Elektrine lite

← Feed

@treyhunner@mastodon.social

Post #4350726

2026-08-03 04:22 UTC

Python Tip #214 (of 365): Use truthiness for integers with caution. I DO rely on truthiness checks for integers, but not always. I would recommend only using them when they seem to improve readability. I find this: if hours: ... More readable than this: if hours > 0: ... But I find this: if number % 2 != 0: ... More readable than this: if number % 2: ... 🧵 (1/2) #Python #DailyPythonTip

Replies (1)

  • @treyhunner@mastodon.social 2026-08-03 04:22

    I find that I appreciate truthiness when it feels like we're asking "does this exist". I think of this question in the case of the hours example above but not for the remainder of division by 2. If you avoid truthiness for numbers altogether, I wouldn't blame you. I rely on truthiness checks heavily for checking non-emptiness and non-noneness, but I rarely use them for checking non-zeroness. It CAN be handy, but I do sometimes find it less readable. 🧵 (2/2)

    Open ##4350725