[Tutor] PEP 572 -- Assignment Expressions

Avi Gross avigross at verizon.net
Tue Nov 27 20:26:04 EST 2018


Ivo,

One thing you could have done is explore with simpler code to see if you can
deduce what is happening.

 If you opened your Python interpreter and tried to see what happens with a
simplified variant like this, what do you get?

if (match = 5) is not None: pass

That might answer your question on why a := might introduce new
functionality.

Something similar would work fine in a language like C/C++. Heck, it is used
often as a concise side effect.

Both languages have an == to be used in expressions but with a meaning of
COMPARE, not set equal to.

I guess someone thought that situations like matching a regular expression
would benefit for the side effect. There are workarounds, of course. As an
example, in SOME cases, the value of the last expression can be found
momentarily in the special variable consisting of a single underscore. But
the expression above probably would evaluate to True, ...

Ovi


-----Original Message-----
From: Python-list <python-list-bounces+avigross=verizon.net at python.org> On
Behalf Of Ivo Shipkaliev
Sent: Tuesday, November 27, 2018 5:48 AM
To: python-list at python.org
Subject: PEP 572 -- Assignment Expressions

   Hello.
   Maybe it's too late for a discussion, but I just couldn't resist.
   I just found out about this new ":=" operator. I need to ask:

   What is the need for this additional ":" to the "="?
   Why:
         if (match := pattern.search(data)) is not None:
               # Do something with match

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

   Assignment expression or assignment statement, it's an assignment, right?
It is very clear to everyone that it's an assignment! Can't it all just be a
"="?
   Thank you very much!


   Kind Regards
   Ivo Shipkaliev
--
https://mail.python.org/mailman/listinfo/python-list



More information about the Tutor mailing list