Skip to content

Instantly share code, notes, and snippets.

@stuartmyles
Created August 9, 2015 21:07
Show Gist options
  • Save stuartmyles/c91202ec18792620a768 to your computer and use it in GitHub Desktop.
Save stuartmyles/c91202ec18792620a768 to your computer and use it in GitHub Desktop.
Python ast code to call a function _bar(), pass in a value and assign the returned value to a variable called "result", i.e. equivalent to result = _bar("theResult")
import ast
# ast code to call a function _bar(), pass in a value and assign the returned value to a variable called "result", i.e. equivalent to
# result = _bar("theResult")
def _bar(theStr):
return theStr
assignresult = ast.Module(body=[ ast.Assign(targets = [
ast.Name(id = 'result', ctx = ast.Store())],
value = ast.Call(func = ast.Name(id='_bar', ctx = ast.Load()), ctx = ast.Load(), args=[ast.Name(id="theResult", ctx = ast.Load())], 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