In our code, comprehensions are sometimes called from an exec inside a function's body. The behavior however, is not similar to other calls to comprehension:
foo = 1
bar = [i for i in (0,) if foo]
print(bar)
-> [0]
LIST_COMPREHENSION = \
"""
foo = 1
bar = [i for i in (0,) if foo]
print(bar)
"""
exec(LIST_COMPREHENSION)
-> [0]
def f():
foo = 1
bar = [i for i in (0,) if foo]
print(bar)
f()
-> [0]
def g():
exec(LIST_COMPREHENSION)
g()
->
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-2-0cc9a6403ba4> in <module>
1 def g():
2 exec(LIST_COMPREHENSION)
----> 3 g()
<ipython-input-2-0cc9a6403ba4> in g()
1 def g():
----> 2 exec(LIST_COMPREHENSION)
3 g()
<string> in <module>
<string> in <listcomp>(.0)
NameError: name 'foo' is not defined