[Python-Dev] Informal educator feedback on PEP 572 (was Re: 2018 Python Language Summit coverage, last part) (original) (raw)

Steven D'Aprano steve at pearwood.info
Mon Jul 2 11:34:12 EDT 2018


On Wed, Jun 27, 2018 at 07:29:52PM -0500, Tim Peters wrote: [...]

For example, if the name is declared "global" in the outer scope, you'll get a compile-time error if you try to declare it "nonlocal" in the contained scope. "parentlocal" adjusts its meaning accordingly, becoming a synonym for "global" in that specific case.

"Parentlocal" is only a thing if we buy into the paradigm that inside comprehensions is a separate "local". And that is only true under two circumstances:

But experimenting with locals() inside comprehensions shows that comprehension-scope isn't a well-defined thing. It already bleeds out of the comprehension, and so would some (but only some!) assignment expressions.

Instead, if we start from the premise that comprehensions (like any other expression) run in the current scope, then there is no need to invent a term "parentlocal". There's just the usual LEGB scopes, plus class (which people usually forget).

With no sublocal scopes (a term we never even had prior to this PEP) assignments inside the comprehension are no more special than assignments inside any other expression. They bind in the current scope, same as always, and keep the sensible identity that these two expressions are exactly equivalent in their visible semantics:

[x:=0, x:=1, x:=2]

[x:=i for i in (0, 1, 2)]

including assignments.

What about the loop variable?

They ARE special, which is completely justified by the Zen:

Although practicality beats purity.

We can take a series of ever-more-detailed explanations, starting from the highest "bird's eye" view and gradually dropping further into the murky details of the implementation when, and if, required:

Only the first two levels of explanation are part of Python the language. The rest is CPython implementation.

-- Steve



More information about the Python-Dev mailing list