On 25 March 2018 at 14:08, Ethan Furman <et...@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 | ncogh...@gmail.com | Brisbane, Australia
_______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/