[Python-Dev] PEP 572: Assignment Expressions

Nick Coghlan ncoghlan at gmail.com
Fri Apr 20 11:58:11 EDT 2018


On 20 April 2018 at 21:25, Christoph Groth <christoph at grothesque.org> wrote:
> 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.

There were a couple of factors at play there. Firstly, python-ideas
and python-dev play different roles in the process, with python-ideas
focused on "Help PEP authors put forward the most compelling proposal
possible", and then python-dev providing the far more stringent filter
of "Do we sincerely believe the long term improvements in language
learnability and code maintainability arising from this change will
outweigh the inevitable near term costs?" (python-ideas does consider
the latter question as well, but we're more willing to spend time on
ideas that only reach the level "Maybe? Depending on your point of
view?").

Secondly, the original PEP proposed sublocal scopes *precisely* to
help preserve that property by limiting the impact of any name binding
side effects to a single statement (albeit an arbtirarily long nested
suite in the case of compound statements).

My own enthusiasm for the idea largely waned after folks successfully
campaigned for "we'd prefer side effects to introducing a new kind of
scope" (and while I'm definitely sympathetic to the "Python's name
lookup scoping rules are already excessively complicated" point of
view, I also think that if "=" and ":=" both target the same kind of
scope, there isn't enough new expressiveness introduced by the latter
to justify the syntactic complexity of adding it).

Cheers,
Nick.

P.S. While I'm not planning to work on it myself anytime soon, I think
the sublocal scoping semantics proposed in earlier versions of PEP 572
would provide a *much* better developer experience for PEP 3150's
"given" clause (which is currently deferred indefinitely, as even I
don't particularly like the current incarnation of that proposal).

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-Dev mailing list