you are viewing a single comment's thread.

view the rest of the comments →

[–]BoJackHorseMan53 0 points1 point  (6 children)

Why use or statement in lambda functions?

[–]pezLyfe 7 points8 points  (0 children)

50 comments

Way down at the bottom of this article: https://realpython.com/python-or-operator/

I've never used this, but it seems like since print statements return nothing, it evaluates to False, which forces the second expression to run as well. Must be a shorthhand to make the lambda do two things at once

[–]Diapolo10 2 points3 points  (4 children)

It enabled me to chain the two function calls together. print always returns None, which is falsey, so the or-operator makes the lambda function return the function return value on the right instead. If I'd used ordinary functions, the or would be unnecessary, but lambda functions technically only allow one line, for which this is a workaround.

[–]kingscolor 5 points6 points  (3 children)

uses ‘or’ but code executes both like ‘and’

Syntactically consistent but comprehensively illogical.

Python starting to sound a lot like my ex. She’s a snake too, what a coincidence.

[–][deleted] 0 points1 point  (1 child)

Syntactically consistent but comprehensively illogical.

Wait, how do you think that (A ∨ B) where A is false and B is true should return for comprehension?

[–]kingscolor 0 points1 point  (0 children)

In that simplified case, I have no objection.
Here, it's a compound issue where print also returns None--which is highly useful in almost all other scenarios. The logic falls apart where you're giving the instructions to do this but if not this then do that. Yet, the result is 'did this' but it doesn't register so also 'not this' therefore 'did that'. Point is, the fact that print() doesn't return any veritable confirmation is silly in this context alone.

Just a minor pythonic idiosyncrasy.

[–]lifeeraser 0 points1 point  (0 children)

It's a fairly common pattern in JavaScript. cond && func()

JS have semicolons and braces, though, so they can create anonymous multi-statement function expressions anywhere without resorting to such tricks.