Python: dynamically create function at runtime February 17, 2023 by Tarik Use exec: >>> exec("""def a(x): ... return x+1""") >>> a(2) 3