On 06/06/2018 03:51 PM, Nick Coghlan wrote:
> On 6 June 2018 at 15:31, Rob Cliffe via Python-Dev <python-dev@python.org 
<mailto:python-dev@python.org>> wrote:
> ...
>     *In other words, it looks as if in Python 3.6.5, the compiled list 
comprehension**
>     **can "see" a pre-existing global variable but not a local one.*
>
> Yes, this is expected behaviour - the two-namespace form of exec (which is 
what you get implicitly when you use it inside a function body) is similar to a 
class body, and hence nested functions
> (including the implicit ones created for comprehensions) can't see the top 
level local variables.


In issue 13557 [1] Amaury gives the following explanation by quoting the documentation [2] "Free variables are not resolved in the nearest enclosing namespace, but in the global namespace" and hints at the same solution that is proposed by Nick.

FWIW in issue 21161 [3] folks have been bitten by this when trying to run a 
list comprehension in pdb.

[1] https://bugs.python.org/issue13557
[2] 
http://docs.python.org/py3k/reference/executionmodel.html#interaction-with-dynamic-features
[3] https://bugs.python.org/issue21161

Xavier

_______________________________________________
Python-Dev mailing list
Python-Dev@python.org
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to