Created
August 9, 2015 20:52
-
-
Save stuartmyles/c7d1ae63af0ba52ac080 to your computer and use it in GitHub Desktop.
Python ast code to call a function _foo() and assign the returned value to a variable called "result", i.e. equivalent to result = foo()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ast | |
# ast code to call a function _foo() and assign the returned value to a variable called "result", i.e. equivalent to | |
# result = _foo() | |
def _foo(): | |
return "It worked!" | |
assignresult = ast.Module(body=[ ast.Assign(targets = [ | |
ast.Name(id = 'result', ctx = ast.Store())], | |
value = ast.Call(func = ast.Name(id='_foo', ctx = ast.Load()), ctx = ast.Load(), args=[], keywords=[])) | |
]) | |
ast.fix_missing_locations(assignresult) | |
co = compile(assignresult, "<ast>", "exec") | |
exec(co) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment