Skip to content

hy.eval called from python function and lfor #2345

Answered by Kodiologist
syntonym asked this question in Q&A
Discussion options

You must be logged in to vote

I think this boils down to the most confusing aspect of Python's exec: "If exec gets two separate objects as globals and locals, the code will be executed as if it were embedded in a class definition", not as a function as you might expect. Notice that

>>> exec('print([b for x in [1]])', globals(), {"b": 3})
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 1, in <module>
  File "<string>", line 1, in <listcomp>
NameError: name 'b' is not defined

and indeed

>>> class C:
...   b = 3
...   a = b + 3
...   c = [b for x in [1]]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 4, in C
  File "<stdin>…

Replies: 3 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Answer selected by syntonym
Comment options

You must be logged in to vote
1 reply
@Kodiologist
Comment options

Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants