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

Nick Coghlan ncoghlan at gmail.com
Sun Mar 25 00:24:11 EDT 2018


On 25 March 2018 at 14:08, Ethan Furman <ethan at stoneleaf.us> wrote:

> Looks like a buggy implementation detail.  Any assignments that happen
> inside a listcomp should be effective only inside the listcomp.
>

No, the fact that the expression defining the outermost iterable gets
evaluated in the outer scope is behaviour that's explicitly tested for in
the regression test suite.

The language reference spells out that this is intentional for generator
expressions, where it has the added benefit of reporting errors in the
outermost iterable expression at the point where the genexp is defined,
rather than at the point where it gets iterated over:
https://docs.python.org/3/reference/expressions.html#generator-expressions

Independently of the pragmatic "getting them to work sensibly at class
scope" motivation, comprehensions inherit those semantics by way of the
intended semantic equivalence between "[x for x in sequence]" and "list(x
for x in sequence)".

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180325/2cfbc505/attachment.html>


More information about the Python-ideas mailing list