1

Is there a way to convert a string to a function in Python while maintaining indentation, newline characters etc.?

For eg., take in "def fn1():\n\tprint("Hello World")"

and convert it to a function:

def fn1():
    print("Hello World)

My use case is to allow a user to pass in a function defined by them as a JSON object which is then used in an otherwise static engine.

An example would be:

def run_user_code(fn_str):
    fn=convert_to_fn(fn_str)
    return fn # or call fn here like fn()

Similar to this question but in Python

12
  • And... you want to call it from the same script? Commented Aug 11, 2022 at 19:59
  • 5
    Can you explain why you want to do this? This sounds like an XY problem to me. Commented Aug 11, 2022 at 20:00
  • 1
    @0x5453 Updated with a use case Commented Aug 11, 2022 at 20:00
  • 1
    Well in that case it's juse exec. But again seems like a strange thing to want to do. Commented Aug 11, 2022 at 20:04
  • 1
    If the user is running this on their own machine, and they're willing and able to write Python code, why don't you just give them a Python API that takes an actual function object, rather than an API that takes a JSON object that is meant to be interpreted as Python source code? Commented Aug 11, 2022 at 20:30

3 Answers 3

3

You can use the python module ast

import ast
def string_to_function(string):
    # parse the string to an ast
    tree = ast.parse(string)
    # get function name
    name = tree.body[0].name # Needs to be changed depending on the input
    # compile the ast to executable code
    code = compile(tree, '<string>', 'exec')
    ns = {}
    # exec the code in a namespace
    exec(code, ns)
    # return the callable function
    return ns[name]

s = "def fn1():\n\tprint('Hello World')"
fn = string_to_function(s)
fn()

Output

Hello World

This code only works if the input is a function.

Or you can just call exec().

c = "def fn2():\n\tprint('Hello World')"
exec(c)
fn2()

This only works if you already know the function name.

Alwayss be careful what code you execute. In this example the User can put any code inside his function, including imports like os or sys, and it will be executed. This can be very dangerous!

Sign up to request clarification or add additional context in comments.

Comments

1

A variation of the answer by noah1400. You can get a dict with a single entry, where the key is the name of the function and its associated value is the function:

string = "def fn2():\n\tprint('Hello World')"
glob = dict()
local = dict()
exec(string, glob, local)
name = list(local)[0]
func = local[name]
print(name)
func()

Result:

fn2
Hello World

Comments

0

It's a bad idea, but you can execute the string as code with

exec('def fn1():\n\tprint("Hello World")')

which defines the function.

How do I execute a string containing Python code in Python?

1 Comment

This assumes that you know the function name in advance.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.