walrus operator
- noun
- Jargon
- The walrus operator lets you assign values inside an if statement in one line.
Examples
-
The walrus operator allows you to assign a variable and return its value in one statement.
-
The walrus operator is a new feature in Python 3.8.
-
Using the walrus operator, she avoided recalculating the list length before the loop.
-
Many style guides discourage the walrus operator because it can make complex expressions harder to read.
-
Have you used the walrus operator in your code?
-
By using the walrus operator, you can simplify your code by making assignments inside expressions.
-
Many developers appreciate how the walrus operator can streamline conditional statements in Python.
Surface Forms
Morphology
walrus + operator
Although 'operator' indicates a kind of computational operator, the modifier 'walrus' (an animal) offers no transparent clue about assignment-in-expression semantics; the term is a community nickname based on the symbol's visual resemblance rather than its function. A B1 learner who knows the words 'walrus' and 'operator' would not be able to infer the specific technical meaning ':=' (assignment within an expression), so the expression is not predictable from its parts.
Etymology
The noun walrus operator gets its name from the symbol :=, which looks like a walrus face, with the ':' as eyes and the '=' like tusks. In code the operator lets you 'assign' a value inside an expression, so you can set a variable and use it in one step.