[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

PEP 572 -- Assignment Expressions

On 11/27/2018 5:48 AM, Ivo Shipkaliev wrote:
>     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 "="?
Did you read the PEP? It answers the question.
>     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

match = pattern.search(data) is not None

returns True or False, which is then assigned to match. On the other 
hand, in

  (match := pattern.search(data)) is not None
match is assigned the result of the search; then that is compared to None.

I am glad to see this added to python. I first encountered embedded assignment in 1969 in the fortran iv compiler for the GE 415 computer. Love at first sight.

Bob Gailer