Can somebody help me understand why the following code fails?
def foo(code):
exec(code)
if __name__ == "__main__":
code = R"""
from typing import List
class Test:
def __init__(a: List):
pass
"""
foo(code)
# exec(code) # this works
I’m using Python 3.9.19
on MacOs.
Also, the following changes make the example work:
- Replacing
List
withlist
- running
exec(code)
beforefoo
- passing
globals()
as locals toexec
- changing the code to:
code = R"""
from typing import List
def boo(a: List):
pass
"""
The code example to run without error.
New contributor
Dano is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.