Summary#
PEP 572 is a feature in Python 3.8 called Assignment Expressions.
Recently, this syntax has caused a lot of controversy in the Python community. Let me explain what this change is about.
First, let me share the introduction from PEP 572:
In most contexts where arbitrary Python expressions can be used, a named expression can appear. This is of the form NAME := expr where expr is any valid Python expression other than an unparenthesized tuple, and NAME is an identifier.
Here are some examples:
if (match := pattern.search(data)) is not None:
# Do something with match
# A loop that can't be trivially rewritten using 2-arg iter()
while chunk := file.read(8192):
process(chunk)
# Reuse a value that's expensive to compute
[y := f(x), y**2, y**3]
This usage does not restrict the position of variables, as shown below:
def foo(answer = p := 42): # INVALID
...
def foo(answer=(p := 42)): # Valid, though not great style
...
So, it can also be done like this:
def foo(answer: p := 42 = 5): # INVALID
...
def foo(answer: (p := 42) = 5): # Valid, but probably never useful
This opens up a hole in the code, and we can expect to see this kind of syntax in various projects soon:
if foo := bar(x):
print(foo)
In simple terms, it is a way to "embed assignment in an expression".
Conclusion#
With the above introduction, you should be able to understand its purpose. Although I personally support this syntax as it makes things more convenient, the focus is not on convenience but on the "complexity" of the syntax.
For beginners who have just started learning Python, it is highly likely that they will confuse the correct usage of Assignment Expressions.
Because of this, a battle has begun. Some people agree with Guido's approval of this sudden feature, while others criticize Python's syntax for not being strictly controlled, leading to more and more unconventional and less readable code.
The proponents argue:
- Just like the f-string feature, people are not looking far enough. This is helpful for the future, etc.
- Isn't this method commonly used in C++?
The opponents argue:
- It goes against the Zen of Python, although the author of the Zen of Python doesn't see it as a big problem.
- This syntax should be heavily restricted in scope to maintain simplicity.
- The general public tends to see ":=" as a definition, not an assignment.
Of course, these comments can all lead to dissatisfaction from the "benevolent dictator" Guido, and eventually his resignation from the Python decision-making team. Personally, I think this is unnecessary.
Until now, it is not clear who is right and who is wrong. It seems that the community has provided some constructive suggestions, but Guido couldn't accept it and decided to step down. Although the Python community is already very large, this will still affect the speed of Python's development.
If you are in a position to introduce a feature and receive criticism and backlash from the community, it may not feel good. If everyone in the community can put themselves in the shoes of the decision-makers and think about it, maybe they can promote and improve without being so radical, and avoid such consequences.