[Python-Dev] PEP 572 semantics

Chris Angelico rosuav at gmail.com
Wed Jul 4 20:25:17 EDT 2018


On Thu, Jul 5, 2018 at 10:20 AM, Steve Dower <steve.dower at python.org> wrote:
> On 04Jul2018 1518, Tim Peters wrote:
>> The only new thing is specifying the scope of `a`, where "local to f"
>> means exactly the same thing as for any other name local to a function
>> today.  So far as the PEP semantics go, it doesn't even matter whether
>> an implementation _does_ implement some form of closure as such.  It
>> just has to provide the visible semantics of _lexically_ nested scopes
>> with indefinite extent, by whatever means it likes best.  That's what
>> "local to f" means (and has meant all along - well, since lexically
>> nested scopes were first introduced).
>
> In that case, please provide more examples of how it should work when
> the assignment expression appears to define a variable in a scope that
> is not on the call stack.

Using an assignment expression in a comprehension is the same as using
an assignment expression outside a comprehension at the exact same
point in the code. I'm not sure what else needs to be explained
further. It's assignment, so it creates a name at that scope.

> For example, what should be returned from this function?
>
> >>> A = 0
> >>> def f(x):
> ...     if x:
> ...         [A := i for i in [1]]
> ...     return A

A would be local to f(). This will either return 1 or raise
UnboundLocalError; it will never return 0.

ChrisA


More information about the Python-Dev mailing list