[Python-Dev] PEP 572: Assignment Expressions

Nick Coghlan ncoghlan at gmail.com
Fri Apr 20 12:08:34 EDT 2018


On 21 April 2018 at 01:49, Chris Angelico <rosuav at gmail.com> wrote:
> On Fri, Apr 20, 2018 at 10:51 PM, Paul Moore <p.f.moore at gmail.com> wrote:
>> 2. I hadn't really thought of the PEP in those terms. Now that you've
>> mentioned it, "Python has never before had syntax that assigns names
>> from within an expression" is quite a significant change, and one the
>> PEP needs to examine the implications of. It's mostly been covered in
>> the discussions by now, of course.
>
> Depending on your definition of "assignment", a lambda function could
> count as a means of assigning a variable in a subexpression. But yes,
> there is no convenient way to assign to something in a wider scope.

We used to sort of have one (Python 2 list comprehensions), and the
behaviour was sufficiently unpopular that Py3 introduced an implicitly
nested scope to keep the iteration variable name from leaking :)

That history is a non-trivial part of why I advocated more strongly
for the original sublocal scoping version of the proposal: with
tighter lexical scoping for expression level assignments, I think we'd
be able to avoid most of the downsides that come from reinstating the
ability for expressions to bind and rebind names.

However, given how those original discussions went, I now think the
only way that option might be successfully pitched to people would be
to propose a statement-level variant of it first (perhaps in the form
of a heavily revised variant of PEP 3150's given clause), and then
only propose "expression level name binding with implicit sublocal
scopes" after the semantics of sublocal scoping were already
established elsewhere.

Cheers,
Nick.

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


More information about the Python-Dev mailing list