[Python-Dev] PEP 572: Assignment Expressions

Christoph Groth christoph at grothesque.org
Fri Apr 20 07:25:02 EDT 2018


Steven Turnbull wrote:
> Christoph Groth writes:
> 
>> Wouldn't it be a pity not to liberate assignments from their boring
>> statement existence?
> 
> Maybe not.  While it would be nice to solve the loop-and-a-half
> "problem" and the loop variable initialization "problem" (not everyone
> agrees these are even problems, especially now that we have
> comprehensions and generator expressions), as a matter of taste I like
> the fact that this particular class of side effects is given weighty
> statement syntax rather than more lightweight expression syntax.

I think that this is the crucial point.  If it is indeed a design
principle of Python that expressions should not have the side-effect of
assigning names, than the whole discussion of PEP 572 could have been
stopped early on.  I didn't have this impression since core devs
participated constructively in the discussion.

> That is, I find statement syntax more readable.

Many people will agree that := is more readable when used in cases where
it's meant to be used (as listed in the PEP).  Your objection seems to
refer to the potential for "clever" misuse, like

i := (a := list(iterator)).index(elem)

instead of

a := list(iterator)
i := a.index(elem)

I think that the ":=" syntax catches the eye and makes it easy to spot
even hidden assignment expressions that shouldn't have been used.

Note that the proposed syntax can be actually *more* readable even when
used as a statement, like in

equal := a == b

Personally, I even slightly prefer

a := 3

to the commonplace

a = 3

because it visually expresses the asymmetry of the operation.  (And no,
Turbo Pascal was not my first programming language. :-)

Christoph


More information about the Python-Dev mailing list