On Jan 19, 2020, at 12:15, David Mertz <[email protected]> wrote:
>
> In contrast, in pure Python, most of that you do is in loops over the
> elements of collections. In that case is does a good job of drawing your eye
> to the fact that a method is called but not assigned to anything. When I see
> `mylist.append(foo)` on a line by itself, the absence of an assigned variable
> drives home "this is basically a statement, called for side-effects" (yes, I
> know in the syntax it is an expression not a statement; but my looser
> cognitive model works that way).
But it is an expression statement. And that works just as well without having
to be mildly incorrect, and it extends even further from there.
Most statements in most Python code mutate one thing. Most of them explicitly
mutate the first thing mentioned (and any expressions within them are usually
nonmutating). Expression statements dont explicitly mutate anything, but
usually at the top level they’re usually either a method call that mutates its
self, or a function call that mutates something implicit in the function, and
subexpressions are usually nonmutating. So, most statements in most Python code
mutate the first thing:
# the only mutation is (probably) to spam:
spam = eggs(cheese)
spam += eggs
spam[eggs] = cheese
spam.eggs(cheese)
# the only mutation is (probably) to the stdout implicit in print:
print(spam)
print(spam.eggs(cheese))
print(spam(eggs))
There are of course exceptions. A from … import statement creates the names at
the end of the statement rather than the start. Mutating methods are only
encouraged to return None, not required. And so on. But the rough guideline
works well enough for skimming most Python code to see “where does x get
changed” in a way that doesn’t work for languages that encourage fluent
mutating style, like JS or Ruby or C++.
What about the walrus operator? The fact that it clearly breaks this is, I
think, a big part of the reason that so many people were against it. But the
fact that it’s usually used only to mutate something that’s only relevant
within the statement is a big part of the reason people actually like it once
they see real examples.
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/2QVSANBME72UBSPOXVAQDIYIXDI6BHXR/
Code of Conduct: http://python.org/psf/codeofconduct/