Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the correct syntax for Walrus operator with ternary operator?

Looking at Python-Dev and StackOverflow, Python's ternary operator equivalent is:

a if condition else b

Looking at PEP-572 and StackOverflow, I understand what Walrus operator is:

:=

Now I'm trying to to combine the "walrus operator's assignment" and "ternary operator's conditional check" into a single statement, something like:

other_func(a) if (a := some_func(some_input)) else b

For example, please consider the below snippet:

do_something(list_of_roles) if list_of_roles := get_role_list(username) else "Role list is [] empty"

I'm failing to wrap my mind around the syntax. Having tried various combinations, every time the interpreter throws SyntaxError: invalid syntax. My python version is 3.8.3.

My question is What is the correct syntax to embed walrus operator within ternary operator?

like image 359
BhaveshDiwan Avatar asked Aug 06 '20 18:08

BhaveshDiwan


People also ask

What is the correct syntax of the ternary operator?

The conditional (ternary) operator is the only JavaScript operator that takes three operands: a condition followed by a question mark ( ? ), then an expression to execute if the condition is truthy followed by a colon ( : ), and finally the expression to execute if the condition is falsy.

What is the syntax of ternary operator in Python?

The ternary operator in Python is nothing but a one-line version of the if-else statement. It provides a way to write conditional statements in a single line, replacing the multi-line if-else syntax.

What is the syntax of ternary operator in C++?

This operator returns one of two values depending on the result of an expression. If "expression-1" is evaluated to Boolean true, then expression-2 is evaluated and its value is returned as a final result otherwise expression-3 is evaluated and its value is returned as a final result.

What is the use of syntax and ternary operator?

The ternary operator is used to execute code based on the result of a binary condition. It takes in a binary condition as input, which makes it similar to an 'if-else' control flow block. It also, however, returns a value, behaving similar to a function.

What is Walrus operator in Python?

What is walrus operator in Python? Walrus operator is a special type of operator that got introduced with Python 3.8. This operator provide a way of assigning variables within an expression with the help of the notation: NAME := expr. The operator is named so because its colon looks like the eyes and = like the tusks of a walrus.

How to use the ternary operator?

The ternary operator is really very simple to use. The ternary operator, as mentioned above, consists of three parts, the condition, the result if true, and the result if false. //ternary operator - example 1 $variable = ($x==1) ? true: false; Let’s say that we apply a higher shipping charge of $15.00 to orders placed in the east.

How to avoid the Walrus operator bugs with assignment expressions?

When introducing assignment expressions, a lot of thought was put into how to avoid similar bugs with the walrus operator. As mentioned earlier, one important feature is that the := operator is never allowed as a direct replacement for the = operator, and vice versa.

Can You rewrite a list comprehension using the Walrus operator?

In this section, you’ve focused on examples where list comprehensions can be rewritten using the walrus operator. The same principles also apply if you see that you need to repeat an operation in a dictionary comprehension, a set comprehension, or a generator expression.


Video Answer


2 Answers

Syntactically, you are just missing a pair of parenthesis.

do_something(list_of_roles) if (list_of_roles := get_role_list(username)) else "Role list is [] empty"

If you look at the grammar, := is defined as part of a high-level namedexpr_test construct:

namedexpr_test: test [':=' test]

while a conditional expression is a kind of test:

test: or_test ['if' or_test 'else' test] | lambdef

This means that := cannot be used in a conditional expression unless it occurs inside a nested expression.

like image 131
chepner Avatar answered Oct 17 '22 15:10

chepner


For someone looking for a short answer or failing to grasp the accepted answer quickly like I did:

>>> variable = foo if (foo := 'parentheses!!') else 'otherwise'
>>> #                 ^                      ^
>>> #                 └──────────────────────┘
>>> variable
parentheses!!
like image 1
Lars Blumberg Avatar answered Oct 17 '22 14:10

Lars Blumberg