623
you are viewing a single comment's thread
view the rest of the comments
[-] maegul@lemmy.ml 12 points 1 year ago* (last edited 1 year ago)

Python, checking in ...

return (a or b)

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 to a if a is "truthy" else b. "Falsy" is empty strings/containers, 0 and None.

[-] juggling_collie@lemmy.ml 13 points 1 year ago

You need to be careful here though. You might not intend for 0 and None to mean the same thing.

[-] Bumblefumble@lemm.ee 3 points 1 year ago* (last edited 1 year ago)

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.

[-] joyjoy@lemm.ee 3 points 1 year ago

Also if a is an empty collection or str, or false.

[-] joyjoy@lemm.ee 5 points 1 year ago

There was PEP 505 to add a none-aware operator, but it lost support.

[-] maegul@lemmy.ml 1 points 1 year ago

That’s a shame, it would have been fitting in “modern” Python along with the walrus and static type system.

this post was submitted on 08 Dec 2023
623 points (96.4% liked)

Programmer Humor

32745 readers
105 users here now

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

founded 5 years ago
MODERATORS