[Python-ideas] Spelling of Assignment Expressions PEP 572 (was post #4)

Chris Angelico rosuav at gmail.com
Sun Apr 15 16:16:46 EDT 2018


On Mon, Apr 16, 2018 at 1:58 AM, Steven D'Aprano <steve at pearwood.info> wrote:
> On Sun, Apr 15, 2018 at 10:21:02PM +1000, Chris Angelico wrote:
>
>> I don't think we're ever going to unify everyone on an arbitrary
>> question of "expression first" or "name first". But to all the
>> "expression first" people, a question: what if the target is not just
>> a simple name?
>>
>> while (read_next_item() -> items[i + 1 -> i]) is not None:
>>     print("%d/%d..." % (i, len(items)), end="\r")
>
> I don't see why it would make a difference. It doesn't to me.

Okay, that's good. I just hear people saying "name" a lot, but that
would imply restricting the grammar to just a name, and I don't know
how comfortable people are with more complex targets.

>> Does this make sense? With the target coming first, it perfectly
>> parallels the existing form of assignment:
>
> Yes, except this isn't ordinary assignment-as-a-statement.
>
> I've been mulling over the question why I think the expression needs to
> come first here, whereas I'm satisfied with the target coming first for
> assignment statements, and I think I've finally got the words to explain
> it. It is not just long familiarity with maths and languages that put
> the variable first (although that's also part of it). It has to do with
> what we're looking for when we read code, specifically what is the
> primary piece of information we're initially looking for.
>
> In assignment STATEMENTS the primary piece of information is the target.
> Yes, of course the value assigned to the target is important, but often
> we don't care what the value is, at least not at first. We're hunting
> for a known target, and only when we find it do we care about the value
> it gets.
> [chomp details]

> It is appropriate for assignment statements and expressions to be
> written differently because they are used differently.

I don't know that assignment expressions are inherently going to be
used in ways where you ignore the assignment part and care only about
the expression part. And I disagree that assignment statements are
used primarily the way you say. Frequently I skim down a column of
assignments, caring primarily about the functions being called, and
looking at the part before the equals sign only when I come across a
parameter in another call; the important part of the line is what it's
doing, not where it's stashing its result.

> [...]
>> >>> items = [None] * 10
>> >>> i = -1
>> >>> items[i := i + 1] = input("> ")
>> > asdf
>> >>> items[i := i + 1] = input("> ")
>> > qwer
>> >>> items[i := i + 1] = input("> ")
>> > zxcv
>> >>>
>> >>> items
>> ['asdf', 'qwer', 'zxcv', None, None, None, None, None, None, None]
>
>
> I don't know why you would write that instead of:
>
> items = [None]*10
> for i in range(3):
>     items[i] = input("> ")
>
>
> or even for that matter:
>
> items = [input("> ") for i in range(3)] + [None]*7
>
>
> but whatever floats your boat. (Python isn't just not Java. It's also
> not C *wink*)

You and Kirill have both fallen into the trap of taking the example
too far. By completely rewriting it, you destroy its value as an
example. Write me a better example of a complex target if you like,
but the question is about how you feel about complex assignment
targets, NOT how you go about creating a particular list in memory.
That part is utterly irrelevant.

>> Are you as happy with that sort of complex
>> expression coming after 'as' or '->'?
>
> Sure. Ignoring the output of the calls to input():

The calls to input were in a while loop's header for a reason.
Ignoring them is ignoring the point of assignment expressions.

ChrisA


More information about the Python-ideas mailing list