What is Walrus Operator in python ? Python 3.8 Version New Features.
💗
Each new version of python adds new features to language. for python 3.8 the bigger change is the addition of assignment expression.
Specifically the " := " operator gives you a new syntax for assigning variable in the middle of expression. This operator is colloquially known as the Walrus operator.
The Walrus operator is officially known as the Assignment Expression Operator.
During early discussion it was dubbed the Walrus Operator because the := syntax resembles the eye and tusks of a sideway walrus.
You may also see the walrus operator referred to as the Colan Equals Operator.
SYNTAX=> Variable:=expression
When to use the Walrus Operator=>
You have two options either going with the assignment operator or the Walrus. But both are used in different scenarios. The assignment operator(=) just assign a value to a variable for future use. Whereas the walrus operator (:=) evaluates the walrus operator expression and assign the result to the variable name.
As we mentioned it's great for simplifying the code in the following situations=>
- Repeated function calls can make your code slower than necessary.
- Repeated statements can make your code hard to maintain.
- Repeated calls that exhaust iterators can make your code overly complex
Comments
Post a Comment