Skip to content

Instantly share code, notes, and snippets.

@JustinSDK
Created August 1, 2018 01:51
Show Gist options
  • Save JustinSDK/6eeb14200bf7194ee4feb23e029e849d to your computer and use it in GitHub Desktop.
Save JustinSDK/6eeb14200bf7194ee4feb23e029e849d to your computer and use it in GitHub Desktop.
modify_ast.py
import ast
code = '''
def add(n1, n2):
return n1 + n2
print(add(3, 4))
'''
class CrazyTransformer(ast.NodeTransformer):
def visit_BinOp(self, node):
node.op = ast.Mult()
return node
node = ast.parse(code)
modified = CrazyTransformer().visit(node)
exec(compile(modified, '<string>', 'exec'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment