this post was submitted on 08 Dec 2023
623 points (96.4% liked)
Programmer Humor
32375 readers
1154 users here now
Post funny things about programming here! (Or just rant about your favourite programming language.)
Rules:
- Posts must be relevant to programming, programmers, or computer science.
- No NSFW content.
- Jokes must be in good taste. No hate speech, bigotry, etc.
founded 5 years ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
view the rest of the comments
Python, checking in ...
Parentheses aren't necessary, I just prefer them for readability.
See python documentation on boolean operators for reference. Short story is
a or b
is an expression that evaluates toa
ifa
is "truthy" elseb
. "Falsy" is empty strings/containers,0
andNone
.You need to be careful here though. You might not intend for
0
andNone
to mean the same thing.So this won't do the intended thing if a is 0.
Edit: Sorry I meant to reply to the parent comment, realising now you already write the exact same thing.
Also if a is an empty collection or str, or false.
There was PEP 505 to add a none-aware operator, but it lost support.
That’s a shame, it would have been fitting in “modern” Python along with the walrus and static type system.