Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use walrus operator in Python 3.7

Why are future imports limited to only certain functionality? Is there no way to get the walrus operator in Python 3.7? I thought this would work, but it doesn't:

from __future__ import walrus

It doesn't work because walrus isn't in the list of supported features:

__future__.all_feature_names
['nested_scopes', 'generators', 'division', 'absolute_import', 'with_statement', 'print_function', 'unicode_literals', 'barry_as_FLUFL', 'generator_stop', 'annotations']

Are there any other alternatives short of using python 3.8?

like image 236
Alex Avatar asked Mar 10 '20 21:03

Alex


People also ask

How do you use the walrus operator in Python?

Python 3.8 introduced a new walrus operator := . The name of the operator comes from the fact that is resembles eyes and tusks of a walrus of its side. The walrus operator creates an assignment expression. The operator allows us to assign a value to a variable inside a Python expression.

Which version of Python has walrus?

The walrus operator was implemented by Emily Morehouse, and made available in the first alpha release of Python 3.8.

Is Python 3.7 backwards compatible?

The latest major language backward incompatible change was Python 3.7 which made async and await real keywords.

Is Python 3.7 or 3.8 better?

Optimizations. In this case, the list uses about 11% less memory in Python 3.8 compared with Python 3.7. Other optimizations include better performance in subprocess , faster file copying with shutil , improved default performance in pickle , and faster operator.

What is the Walrus operator in Python?

The walrus operator is a new syntax that is only available in Python 3.8 and later. This means that any code you write that uses the := syntax will only work on the most recent versions of Python. If you need to support older versions of Python, you can’t ship code that uses assignment expressions.

How to avoid the Walrus operator bugs with assignment expressions?

When introducing assignment expressions, a lot of thought was put into how to avoid similar bugs with the walrus operator. As mentioned earlier, one important feature is that the := operator is never allowed as a direct replacement for the = operator, and vice versa.

How do you assign values to Walrus in Python?

Line 1 shows a traditional assignment statement where the value False is assigned to walrus. Next, on line 5, you use an assignment expression to assign the value True to walrus. After both lines 1 and 5, you can refer to the assigned values by using the variable name walrus.

What is @Walrus-operator?

Walrus-operator is another name for assignment expressions. According to the official documentation, it is a way to assign to variables within an expression using the notation NAME := expr.


Video Answer


2 Answers

If the version of Python you're using doesn't contain an implementation of a feature, then you cannot use that feature; writing from __future__ import ... cannot cause that feature to be implemented in the version of Python you have installed.

The purpose of __future__ imports is to allow an "opt-in" period for new features which could break existing programs. For example, when the / operator's behaviour on integers was changed so that 3/2 was 1.5 instead of 1 (i.e. floor division), this would have broken a lot of code if it was just changed overnight. So both behaviours were implemented in the next few versions of Python, and if you were using one of those newer versions then you could choose the new behaviour with from __future__ import division. But you were only able to do so because the version of Python you were using did implement the new behaviour.

The walrus operator was introduced in Python 3.8, so if you are using a version prior to 3.8 then it does not contain an implementation of that operator, so you cannot use it. There was no need to use __future__ to make the walrus operator "opt-in", since the introduction of a new operator with new syntax could not have broken any existing code.

like image 130
kaya3 Avatar answered Sep 19 '22 13:09

kaya3


You can read the PEP that introduced __future__ for insight. Primarily,

From time to time, Python makes an incompatible change to the advertised semantics of core language constructs, or changes their accidental (implementation-dependent) behavior in some way. While this is never done capriciously, and is always done with the aim of improving the language over the long term, over the short term it's contentious and disrupting.

The walrus operator is not a backwards-incompatible change: it changes nothing about the meaning of code that was already "working". := was just a syntax error before.

So adding it to __future__ was never even considered. You might object that, say, "with" statements were similarly brand new, but that's not entirely so: "with" was not a reserved word, and its introduction could potentially break working code that used "with" as an identifier.

So, sorry, use 3.8 or you're out of luck. Don't shoot the messenger ;-)

like image 29
Tim Peters Avatar answered Sep 20 '22 13:09

Tim Peters