[issue36070] Enclosing scope not visible from within list comprehension

Eric V. Smith report at bugs.python.org
Thu Feb 21 22:04:54 EST 2019


Eric V. Smith <eric at trueblade.com> added the comment:

I suspect Nathan is seeing this problem at class scope. This is a well known issue:

>>> class C:
...   from random import random
...   out = [random() for ind in range(3)]
...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 3, in C
  File "<stdin>", line 3, in <listcomp>
NameError: name 'random' is not defined
>>>

It is not related to the list comprehension, but to the class scope. See the last paragraph of https://docs.python.org/3/reference/executionmodel.html#resolution-of-names

But I agree with Zach about needing an example that fails.

----------
nosy: +eric.smith

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue36070>
_______________________________________


More information about the Python-bugs-list mailing list