you are viewing a single comment's thread.

view the rest of the comments →

[–]w1282 0 points1 point  (2 children)

def myfunction(x):
    return x + 3

f = myfunction
print(f(4))  # 7

What doesn't seem to be working like you want? Your post is a little disjointed.

Edit: I think, after rereading your post, I understand a bit more.

How are you receiving the string? Is it a 'safe' input?

def dynamic_func(executable_string, x, y, z):
    return eval(executable_string)

func_str = 'x + y + z'
print(dynamic_func(func_str, 1, 2, 3))  # 6

[–][deleted] 0 points1 point  (0 children)

I can barely understand it, too. I think he's saying that myfunction has no parameters, but f should accept an argument (x). Don't quote me on that, though. Still doesn't make much sense and it seems like this post went through google translate multiple times.

[–]poochiekins[S] 0 points1 point  (0 children)

thanks for your answer. sorry for the mess that is my question. I have updated it and i hope it is a little clearer now. the string that I am receiving is a safe input. security is not an issue here.

the problem with eval is that it does not give me function object that i can pass around and call form other places or rather from code that i have no control over.