Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Python 3 – How to Use Assignment Expressions

DZone's Guide to

Python 3 – How to Use Assignment Expressions

While this feature isn't currently included in Python, the core developers (including Guido van Rossum!) have proposed it should be added to 3.8. What do you think?

· Big Data Zone ·
Free Resource

The open source HPCC Systems platform is a proven, easy to use solution for managing data at scale. Visit our Easy Guide to learn more about this completely free platform, test drive some code in the online Playground, and get started today.

I recently came across PEP 572, which is a proposal for adding assignment expressions to Python 3.8 from Chris Angelico, Tim Peters, and Guido van Rossum himself! I decided to check it out and see what an assignment expression was. The idea is actually quite simple. The Python core developers want a way to assign variables within an expression using the following notation:

NAME := expr

This topic has had a LOT of arguments about it and you can read the details on the Python-Dev Google group if you want to. I personally found that reading through the various pros and cons put forth by Python’s core development community to be very insightful.

Regardless, let’s look at some of the examples from PEP 572 to see if we can figure out how you might use an assignment expression yourself.

# Handle a matched regex
if (match := pattern.search(data)) is not None:
    ...

# A more explicit alternative to the 2-arg form of iter() invocation
while (value := read_next_item()) is not None:
    ...

# Share a subexpression between a comprehension filter clause and its output
filtered_data = [y for x in data if (y := f(x)) is not None]

In these 3 examples, we are creating a variable in the expression statement itself. The first example creates the variable, match, by assigning it the result of the regex pattern search. The second example assigns the variable, value, to the result of calling a function in the while loop’s expression. Finally, we assign the result of calling f(x) to the variable y inside of a list comprehension.

One of the most interesting features of assignment expressions (to me at least), is that they can be used in contexts that an assignment statement cannot, such as in a lambda or the previously mentioned comprehension. However, they also do NOT support some things that assignment statements can do. For example, you cannot use it to target multiple assignments:

x = y = z = 0  # Equivalent: (x := (y := (z := 0)))

You can see a full list of differences in the PEP.

There is a lot more information in the PEP that covers a few other examples, talks about rejected alternatives and scope.

Managing data at scale doesn’t have to be hard. Find out how the completely free, open source HPCC Systems platform makes it easier to update, easier to program, easier to integrate data, and easier to manage clusters. Download and get started today.

Topics:
big data ,python ,assignment expressions

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}