could anyone give me an advice about this behaviour? The next code runs fine:
import ast
node = ast.parse('def nsd(a, b):\n if b == 0: return a \n return nsd(b, a%b)\n\nprint nsd(18,15)')
obj = compile(node, filename="<ast>", mode="exec")
exec obj
But when I do the same inside a function:
import ast
def foo():
node = ast.parse('def nsd(a, b):\n if b == 0: return a \n return nsd(b, a%b)\n\nprint nsd(18,15)')
obj = compile(node, filename="<ast>", mode="exec")
exec obj
foo()
It raises an error:
Traceback (most recent call last):
File "C:/Users/Vectoun/PycharmProjects/untitled3/test.py", line 9, in <module>
foo()
File "C:/Users/Vectoun/PycharmProjects/untitled3/test.py", line 7, in foo
exec obj
File "<ast>", line 5, in <module>
File "<ast>", line 3, in nsd
NameError: global name 'nsd' is not defined
I would like to be able to run this inside a function. Does anyone know how to solve this?
If I'm not mistaken, the issue here is that when using exec
you're still bound to a scope.
So running the AST code outside of a method will define nsd
as global method, while running the AST code within foo()
will make nsd
a member of foo
's direct scope, so the invocation will fail on looking up the method.
You can fix it by first defining nsd
as a global:
import ast
def foo():
code = """global nsd
def nsd(a, b):
if b == 0:
return a
return nsd(b, a%b)
print nsd(18,15)
"""
node = ast.parse(code)
obj = compile(node, filename="<ast>", mode="exec")
exec obj
foo()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With