Python: dynamically create function at runtime

Use exec:

>>> exec("""def a(x):
...   return x+1""")
>>> a(2)
3

Leave a Comment