What is Python Assignment Expressions 3.8?

Asked

Viewed 281 times

18

As assignment Expressions are defined in the PEP 572 which has been approved to be implemented in version 3.8 of Python.

  • But what are the assignment Expressions and when they should be used?
  • What kind of problem your implementation seeks to solve?
  • Why has its implementation caused controversy in the Python community? It goes against the "principles" of the language?
  • 2

    I suppose you’re working out a pretty cool answer yourself there -

  • 1

    @jsbueno in a way, but feel invited to respond as well.

1 answer

15


The answer goes by levels.

  • Simple concept:
  • Complete concept:
  • Example:
  • How to use:
  • References:

Simple concept:

We can say in a way " "simple" that is to define a variable and pass it as value at the same time.

Ex.:

# $ entrada do código 
# > saída do código 

$ a = 1
>
$ a
> 1

So what you can do is put the two things together in a single line. Getting like this:

$ a := 1    # Você definiu a variável e ao mesmo tempo ela passa o valor adiante.
> 1

Complete concept:

Abstract:

It is a proposal to define variables as an expression. ( NAME := expr )

  • added a new exception "Targetscopeerror".

Motivation:

Naming the result of an expression is an important part of programming, thus allowing it to be used in place of a long expression and allowing reuse.

...
a = foo(x)
baa(a)
cee(a)
...

Now it may be :

...
baa(a:=foo(x))
cee(a)
...

The real importance:

I will not quote directly here because it is great and it is good to read everything, the link is at the end of the answer. If anyone wants I summarize and change here. And yes had discussions if this is pythonic.

Syntax:

Many arbitrary contexts in which Python expressions can be used. This is the form of NAME := expr where expr is a valid Python expression as a tuple without parentheses and NAME is the identifier.

$ a = 3,1
$ type(a)
> <class 'tuple'>

regex

if (match := pattern.search(data)) is not None:
    # Do something with match

Loop cannot be easily rewritten using 2 iterative arguments.

while chunk := file.read(8192):
   process(chunk)

Analyzing value is computationally expensive

[y := f(x), y**2, y**3]

Share the sub-expression between a filter and an "output".

filtered_data = [y for x in data if (y := f(x)) is not None]

Exceptional cases: (Invalid and valid but not recommended.)

Tomorrow put calmly, because of my schedule, but has the link reference PEP 572 at the end of the answer. : ) If to use recommend reading.


Example:

If anyone wants, I create a more complex example, but I will leave one that I found very simple but demonstrative that is in the PEP itself.

# Computar uma lista de somas parciais e retornar o total
total = 0
partial_sums = [total := total + v for v in values]
print("Total:", total)

# Legal né :)

Reference:

PEP 572 -- Assignment Expressions

Example of use - VIDEO - EN

Sorry for the mistakes, whatever I can improve, just say in the comments.

See you around :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.