Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can assignment expressions be enabled in Python 3.7 using __future__?

Python 3.8 introduces assignment expressions, described in PEP 572. Is there a way to test this new feature in Python 3.7.x?

In the past, new language features have been backported to earlier Python versions using __future__ imports.

  • Is there a __future__ import for assignment expressions?
  • If yes, what is the feature name?
  • If no, are there plans to add it? (3.7 is going to be around for a while)
like image 607
Daniel Mahler Avatar asked Jul 30 '18 01:07

Daniel Mahler


People also ask

How do you assign an expression in Python?

Assignment expression are written with a new notation (:=) . This operator is often called the walrus operator as it resembles the eyes and tusks of a walrus on its side. The assignment expression allows you to assign True to walrus , and immediately print the value.

Why can't I use an assignment in an expression in Python?

The error is a simple typo: x = 0, which assigns 0 to the variable x, was written while the comparison x == 0 is certainly what was intended.

Can we use assignment operator in list comprehension?

We can also use assignment expressions in list comprehensions. List comprehensions allow you to build lists succinctly by iterating over a sequence and potentially adding elements to the list that satisfy some condition.

What is this := in Python?

There is new syntax := that assigns values to variables as part of a larger expression.


1 Answers

There is no __future__ import for assignment expressions in Python 3.7 – and adding one in a micro (or "bugfix") release is prohibited by PEP 6:

Prohibitions

Bug fix releases are required to adhere to the following restrictions:

  1. There must be zero syntax changes. All .pyc and .pyo files must work (no regeneration needed) with all bugfix releases forked off from a major release.

Applicability of Prohibitions

The above prohibitions and not-quite-prohibitions apply both for a final release to a bugfix release (for instance, 2.4 to 2.4.1) and for one bugfix release to the next in a series (for instance 2.4.1 to 2.4.2).

Since assignment expressions constitute a change to the syntax of Python, there's no way they can be added to a future 3.7.x release of Python without breaking this prohibition.

like image 125
Zero Piraeus Avatar answered Oct 11 '22 22:10

Zero Piraeus