3

I have several functions in the database as a strings and there is a main function that should take these functions from the database and execute them as functions, and not as text.

How I can transform a string (which describes a function) into a function with arguments and execute it in Python?

I'm tried exec but I can't return something from function and use it.

For example my function:

some_function = """
def my_random_function(some_param):
    print(some_param)
    return time.time()
"""
2
  • Hey, @Nataly Firstova, try this: stackoverflow.com/questions/7719466/… Commented Nov 19, 2021 at 13:32
  • Function defined by exec can work like normal function, and of course you can also get the return values. Make sure you have import all the essential libraries (time in your example). PS: I will post some code for you in answer Commented Nov 19, 2021 at 13:35

2 Answers 2

3

100% Never do this ever under any circumstances disclaimer...

the exec statement:

code = """
def f(x):
    x = x + 1
    return x

print('This is my output.')
print(f(my_var))
"""

exec(code, {}, {"my_var": 1})

output:

This is my output.
2
Sign up to request clarification or add additional context in comments.

9 Comments

100% Never do this ever under any circumstances disclaimer... - why? I just want to put all functions in DB, and change it without server reload
It is very insecure and unreliable. See here for someone wanting to do this in PHP: stackoverflow.com/a/14653229/6014330
Maybe you know better way for it? I want to add end edit my functions in DB (via frontend editor) and execute it in main function on server (without redeploy)
That's exactly the problem. what you want to do is inherently dangerous. Its like asking to log into your bank account without a username and password. You cant do it securely.
The whole point of CI/CD is so that you can make changes, test them, validate it etc then promote to an environment and test again... You should not be able to change code on the fly without a deployment.
|
0

Function defined by exec can work like normal function, and of course you can also get the return values. Make sure you have import all the essential libraries (time in your example)

import time

some_function = """
def my_random_function(some_param):
    print(some_param)
    return time.time()
"""

exec(some_function)
return_value = my_random_function(3)
print(return_value)

output

3
1637328869.3345668

4 Comments

I got IDE error on it return_value = my_random_function(3)
What is the error? Can you provide what the error said?
Unresolved reference "my_random_function"
I guess you are using Pycharm. It is the Pycharm error since Pycharm needs to know where to find a function, and it can not find a function inside a string. Python can run it smoothly because the function will appear after the exec run.

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.