Assignment expressions are the latest addition in Python 3.8. Assignment expressions are also known as walrus operator because it resembles the eyes and tusks of a walrus :=. This operator has been available in other languages but not in Python.

Resignation of Guido van Rossum

Assignment Expressions

Guido van Rossum is the creator of the Python programming language, if you didn’t know.

From what I know most of the Python core developers were against this and this feature was the reason for Guido van Rossum to resign from his leadership of this project. This is what he had to say:

“Now that PEP 572 is done, I don’t ever want to have to fight so hard for a PEP and find that so many people despise my decisions. I would like to remove myself entirely from the decision process. I’ll still be there for a while as an ordinary core dev, and I’ll still be available to mentor people — possibly more available. But I’m basically giving myself a permanent vacation from being BDFL, and you all will be on your own.”

Guido van Rossum

Assignment Expressions

The notation for the assignment expression is mentioned below

(name := expr)

With assignment expressions you can combine two statements into one. The walrus operator lets you assign and return a value in same expression. Here is an example of how assignment expressions are different:

# Assignment statement
# Assigns value 10 to x
i = 10
# Assignment expression
# Assigns value 10 to x and returns 10
(i := 10)

To help you better understand here is a piece of code which we will optimize with the help of walrus operator:

items = ["a", "b", "c", "d", "e"]
if len(items) < 10 :
    print(f"The list just has {len(items)} items.")

If you notice in the above code, we had to use len(items) twice. This can be avoided with the new assignment expressions.

items = ["a", "b", "c", "d", "e"]
if (size := len(items)) < 10 :
    print(f"The list just has {size} items.")

In the above example, the walrus operator checks and compares the length of items and assigns the length to size which can be used further.

Parenthesis are important

If you have been working in python, you must know python doesn’t need parenthesis very often. But with walrus operator, they are important for the correct assignment and evaluation of the expression.

To understand that you can just run the code below which has expression with and without parenthesis.

items = ["a", "b", "c", "d", "e"]
if (size := len(items)) < 10 :
    print(size)
# Outputs: 5
if size := len(items) < 10 : # Notice the missing parenthesis
    print(size)
# Outputs: True

Your thoughts?

Now you know that this feature has come after a lot of criticism and the step down of the legend himself. Share your thoughts in comments.

Also Read: Convert Images to Pdf in Java

Share:

administrator

I am a full stack software engineer and a blogger. Primarily, I work on Python, Java and React.js. I am an autodidact with a strong passion for new technologies. I love to build new things from the ground up. I have about 7 years of dynamic experience gained by working in early stage startups to mid-sized organizations in an Agile environment.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.