[Python-ideas] PEP 572: Assignment Expressions (post #4)

Steven D'Aprano steve at pearwood.info
Fri Apr 13 08:22:10 EDT 2018


On Wed, Apr 11, 2018 at 11:50:44PM +1000, Chris Angelico wrote:

> > Previously, there was an alternative _operator form_  `->`  proposed by
> > Steven D'Aprano. This option is no longer considered? I see several
> > advantages with this variant:
> > 1. It does not use `:` symbol which is very visually overloaded in Python.
> > 2. It is clearly distinguishable from the usual assignment statement and
> > it's `+=` friends
> > There are others but they are minor.
> 
> I'm not sure why you posted this in response to the open question, but
> whatever. The arrow operator is already a token in Python (due to its
> use in 'def' statements) and should not conflict with anything;
> however, apart from "it looks different", it doesn't have much to
> speak for it.

On the contrary, it puts the expression first, where it belongs 
*semi-wink*.

The expression is the most important part of the assignment expression, 
and because we read from left to right, it should come first. Let's take 
a simple example:

    pair = (first_value := x + y + z,
            a + b + first_value
            )

What's the first item of the pair? If you're like me, and I think most 
people are similar, when skimming the code, you read only far across 
each line to get an idea of whether it is relevant or not. In this case, 
when skimming, you have to read past the name, past the assignment 
operator, and only then do you see the relevant information.

Contrast:

    pair = (x + y + z -> first_value,
            a + b + first_value
            )


Now you need only read *up to* the assignment operator.

Now clearly a careful and detailed reading of the code requires just as 
much work either way, but we often skim code, especially trying to 
identify the section that needs careful reading.

I know that it is a long standing convention in programming and 
mathematics to write assignments with the variable on the left, but when 
I'm sketching out code on paper, I often *start* with the expression:

    x + y + z

and only then do I go back and squeeze in a name binding on the left. 
(Especially since half the time I'm not really sure what the variable 
should be called. Naming things is hard.) Or I just draw in an arrow 
pointing to the name on the right:

    x + y + z ----> name



> The arrow faces the other way in languages like Haskell,

Indeed, but in R, it faces to the right. (Actually, R allows both 
direction.) There's also apparently a language BETA which uses -> for 
assignment, although I've never used it.

HP calculator "RPN" language also includes a -> assignment operator for 
binding named parameters (taken off the stack) inside functions, except 
they use a custom encoding with an arrow symbol, not a literal 
hyphen+greater-than sign.

Likewise for TI Nspire calculators, which also use an right-pointing 
arrow assignment operator. (They also have a Pascal-style := operator, 
so you're covered both ways.) This comes from various dialects of 
calculator BASIC.


-- 
Steve


More information about the Python-ideas mailing list