Skip to content

Instantly share code, notes, and snippets.

@mistivia
Last active January 26, 2025 03:34
Show Gist options
  • Select an option

  • Save mistivia/cd33719eaa9e741f298337a9a845be0c to your computer and use it in GitHub Desktop.

Select an option

Save mistivia/cd33719eaa9e741f298337a9a845be0c to your computer and use it in GitHub Desktop.
create multi-line lambda from string in Python
def str_lambda(code, name = '_'):
env = {}
eval("exec(code, env)", {"code": 'def ' + name + code, "env": env})
return env[name]
f = str_lambda("""(a, b):
return a + b""")
print(f(1, 2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment