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.
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:
is an assignment, but
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:
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?:
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:
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.
More InformationPEP 572 -- Assignment Expressions Related ArticlesGuido van Rossum Quits As Python BDFL Python 3 For Science - A Survey 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.
Comments
or email your comment to: comments@i-programmer.info |
Last Updated ( Friday, 13 July 2018 ) |