[Python-ideas] PEP 572: Statement-Local Name Bindings

Chris Angelico rosuav at gmail.com
Wed Feb 28 15:00:09 EST 2018


On Thu, Mar 1, 2018 at 6:35 AM, Brendan Barnwell <brenbarn at brenbarn.net> wrote:
> On 2018-02-28 07:18, Chris Angelico wrote:
>>
>> Except that assignment is evaluated RHS before LHS as part of a single
>> statement. When Python goes to look up the name "a" to store it (as
>> the final step of the assignment), the SLNB is still active (it's
>> still the same statement - note that this is NOT expression-local), so
>> it uses the temporary.
>
>
>         Wait, so you're saying that if I do
>
> a = (2 as a)
>
>         The "a = " assignment assigns to the SLNB, and so is then discarded
> after the statement finishes?
>
>         That seems very bad to me.  If there are SLNBs with this special
> "as" syntax, I think the ONLY way to assign to an SLNB should be with the
> "as" syntax.  You shouldn't be able to assign to an SLNB with regular
> assignment syntax, even if you created an SNLB with the same name as the LHS
> within the RHS.

That seems a reasonable requirement on the face of it, but what about
these variants?

a = (x as a)
a[b] = (x as a)
b[a] = (x as a)
a[b].c = (x as a)
b[a].c = (x as a)

Which of these should use the SLNB, which should be errors, which
should use the previously-visible binding of 'a'?

It wouldn't be too hard to put in a trap for assignment per se, but
where do you draw the line? I think "a[b] =" is just as problematic as
"a =", but "b[a] =" could be useful. Maybe the rule could be that
direct assignment or mutation is disallowed, but using that value to
assign to something else isn't? That would permit the last three and
disallow only the first two.

ChrisA


More information about the Python-ideas mailing list