Re: [Tutor] PEP 572 -- Assignment Expressions

2018-11-28 Thread Steven D'Aprano
On Tue, Nov 27, 2018 at 08:26:04PM -0500, Avi Gross wrote:
> Ivo,

You've replied to the wrong mailing list. Ivo wrote to Python-List, not 
Tutor.

But now that you've raised the issue...


[Ivo asked]
>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
> "="?

It seems that Ivo didn't read the PEP because that is discussed:

https://www.python.org/dev/peps/pep-0572/#id32

Despite Ivo's statement that it is "clear to everyone", the problem is 
that whenever you see an assignment x=y in an assignment, there's no 
obvious way of telling whether they meant assignment or it was a 
mistyped equality test x==y. So it is not clear at all.

In C, this confusion between = and == is a frequent source of serious 
bugs, including one attempt at inserting a back-door into the Linux 
kernel:

https://freedom-to-tinker.com/2013/10/09/the-linux-backdoor-attempt-of-2003/

https://lwn.net/Articles/57135/


Requiring the colon x:=y makes it less likely to mess up the assignment 
or comparison.



-- 
Steve
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] PEP 572 -- Assignment Expressions

2018-11-28 Thread Avi Gross
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  On
Behalf Of Ivo Shipkaliev
Sent: Tuesday, November 27, 2018 5:48 AM
To: python-l...@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

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor