Last active
January 26, 2025 03:34
-
-
Save mistivia/cd33719eaa9e741f298337a9a845be0c to your computer and use it in GitHub Desktop.
create multi-line lambda from string in Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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