The body behind the Python programming language has moved closer to implementing a contentious feature that led the language’s founder to resign his leadership role.
PEP 572 would introduce a feature called Assignment Expressions to the language, a change aimed at making it possible to write more efficient code in Python.
However, the proposal sparked various disagreements, a key one being whether the new feature would harm the readability of the code, the ability for humans to understand what the code is doing, generating a sizeable backlash when it was mooted last year.
Such was the scale of furore that Python’s founder Guido van Rossum stepped back from being the language’s ‘Benevolent dictator for life’, a tongue-in-cheek reference to his role guiding the language’s development.
Blaming the negative reaction to the PEP, the name given to proposals for a change to Python, he said: “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.”
Now PEP 572 is inching closer to release, with this week seeing the release of an early development build of Python, Python 3.8.0 alpha 2, which includes the proposed change. The feature is still some way from final release, however, with another two alpha releases planned, followed by a tranche of beta builds, before the expected final release of Python 3.8 in October this year.
In an attempt to stem the criticism of PEP 572, the proposal gives simple code examples of how Assignment Expressions will be useful, cautioning: “During the development of this PEP many people (supporters and critics both) have had a tendency to focus on toy examples on the one hand, and on overly complex examples on the other.”
The PEP goes on to demonstrate how a Python developer could use an Assignment Expression to reduce the number of lines of code written and, and in some cases executed, to carry out several instructions.
For example, to check whether a string of characters are found in a text using a regular expression, and then take some action based on whether there is a match, the following Python code could be used:
match1 = pattern1.match(data)
match2 = pattern2.match(data)
result = match1.group(1)
result = match2.group(2)
result = None
Whereas the Assignment Expression would allow much of the code above to be replaced with the following line, where multiple operations are combined.
if (match := pattern.search(data)) is not None:
# Do something with match
While the first example could be written more efficiently using existing Python code, the argument for Assignment Expressions is that they provide a way of combining several operations into a single line, making code more efficient to write and, in some cases, to execute.
The other changes in the proposed in Python 3.8.0 alpha 2 can be found on here.
Python is an increasingly popular language for software developers to learn, driven largely by its growing use in the field of data science and machine learning, which is challenging web development as the most common use of Python.
The web is full of free tutorials and guides for learning Python, as you can see from TechRepublic’s round-up of useful sites and videos and code examples available via the online code repository GitHub.