Python Passion For Assignment Expressions - PEP 572
Written by Mike James   
Wednesday, 11 July 2018

You would think with a language as old as Python there wouldn't be much left to get excited about, but over the past few months PEP-572, a proposal to add a new feature, has been raising the blood pressure of the Python community.

python3

 

Ordinarily this situation would result in stalemate with the differing factions feeling bad about the possibility of losing and either implementing or not implementing the proposal. In Python's case the existence of a BDFL (Benevolent Dictator For Life) short circuited what could have otherwise lingered on for many more months. Guido van Rossum settled the matter by accepting the PEP.

On the 2nd of July he wrote

"Thank you all. I will accept the PEP as is. I am happy to accept *clarification* updates to the PEP if people care to submit them as PRs to the peps repo, and that could even (to some extent) include summaries of discussion we've had, or outright rejected ideas. But even without any of those I think the PEP is very clear so I will not wait very long (maybe a week)."

This announcement came very suddenly, so much so that many participants in the discussion almost didn't notice or believe it. For example; on July 3rd Victor Stinner wrote:

"I see more and more articles ("on the Internet") saying that Guido van Rossum already accepted the PEP. Is the PEP already accepted or will be accepted?"

The answer is that it is accepted. I too was surprised, and slightly disappointed, not because I think it's a bad idea, but because I hadn't made up my mind and was looking forward to more high quality discussion.

Even now some are so strongly opposed that they are vowing not to allow the construct in their code when it is introduced in Python 3.8. There is even criticism of Guido for being more D than B in BDFL. Personally I don't see this and I think Python is the stronger for strong leadership. In other language communities the argument would have gone on forever with both sides unwilling to give in and progress being held up by the polarity.in the community. It does make you wonder what might happen to Python without Guido.

UPDATE July 12th: Guido van Rossum Quits As Python BDFL

So what is the fuss all about?

The idea is simple enough - allow assignment within expressions.

Currently assignment in Python is a statement.

This means:

A=B

is an assignment, but

if A==B:

is a test for equality.

The PEP introduces a new assignment operator := which allows assignment within expressions as is possible in some other languages. 

Now you can write things like:

while data:=f.read(100):
   print(data)

This is probably the archetypal use of in-expression assignment. The assignment is a side effect of the expression which evaluates to either data which is  truthy or false if there is no data to read. That is, the expression is used to stop the while loop, but it is also made available to the body of the loop.

The principle is that the value of the expression is assigned, and also returned, to whatever is using it as if the assignment never happened..

This can become more complicated than you might imagine given the range of places that expressions can be used. For example, what do you think happens here?:

myfunction(x=(y:=f(z))

In most cases, see the PEP for the exception, the scope of any variable introduced in an expression assignment is the current scope. So in this case y is local to the code that myfunction is called in and x is a keyword parameter of the function.

Basically, an expression assignment is useful whenever you need to hang on to the result of an expression while it is being used for some other purpose. I quote an example from the PEP:

results = [(x, y, x/y)
       for x in input_data if (y := f(x)) > 0]

The order of evaluation might well worry a beginner, but not much more than the comprehension without the assignment.

The PEP has been accepted but is not completely finished - it needs polishing. It will be interesting to see if the hostility continues once the feature is implemented.

 python3

  • Mike James is the author of Programmer's Python: Everything is an Object published this month by I/O Press as part of the  I Programmer Library. With the subtitle "Something Completely Different" this is for those who want to understand the deeper logic in the approach that Python 3 takes to classes and objects.

     

More Information

PEP 572 -- Assignment Expressions

Related Articles

Guido van Rossum Quits As Python BDFL

Python 3.7 Released

Python Development Trends

What Makes Python Special?

Python 3 For Science - A Survey

Jupyter Receives ACM Award

Free Version of PyCharm Python IDE

Getting Started with Python (Draft book extract from Programmer's Python)

To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.

 

Banner


Copilot Improves Code Quality
27/11/2024

Findings from GitHub show that code authored with Copilot has increased functionality and improved readability, is of better quality, and receives higher approval rates than code authored without it.

 [ ... ]



Firefox 1.0 Released 20 Years Ago
10/11/2024

A news item with the headline "Firefox browser takes on Microsoft" from 20 years ago has attracted renewed attention. It was originally published on the BBC News website on November 9th, 2004 rec [ ... ]


More News

espbook

 

Comments




or email your comment to: comments@i-programmer.info

Last Updated ( Friday, 13 July 2018 )