Skip to content
Advertisement

Why can’t I assign lambda to the variable using ternary operator

When I assign anonymous function to the variable using ternary operator with the true condition, I can call the variable as a normal function.

JavaScript

But when the condition is false and the function to be assigned is after the word else, I get something like this. I have no idea what mechanisms stand behind such a weird behaviour. Is there any syntax nuances? Or any possibility to use ternary operator in cases like this one?

JavaScript

Advertisement

Answer

You need to use parentheses to indicate your meaning.

You mean

JavaScript

but you have

JavaScript

The ... if ... else ... operator has higher precedence than lambda. See https://docs.python.org/3/reference/expressions.html#operator-precedence.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement