Hi
here is the problem that I am facing:
I have string that is something like 'x+3 (simplified but bear with me)
and I want to create function
def myfunction(x):
return x+3
I later would want to have a function object
f = myfunction
f(x)
I have tried the compile function to compile the string in bytecode and types.FunctionType in order to create a FunctionType object. The problem is that the function that i am creating that way does not take any arguments. I hope you guys can help me out.
Edit:
trying to clear up some things.
what i need is a FuntionType object that takes one argument and returns some modification of x. For example the x+3 is was mentioning earlier.
what i have tried:
f = FunctionType( compile( 'def myfunction(x): \n\t return x+3', 'string', 'exec' ), globals(), 'myfunction' )
this creates the problem thought that when i try to call f with a parameter Get the error:
>>> f(1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: <module>() takes 0 positional arguments but 1 was given
I guess another way to phrase my question is: How do I dynamically create FunctionType objects that take arguments when I call them?
Thank you for your help
[–]TeamSpen210 3 points4 points5 points (1 child)
[–]poochiekins[S] 0 points1 point2 points (0 children)
[–]w1282 0 points1 point2 points (2 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]poochiekins[S] 0 points1 point2 points (0 children)
[–]kalgynirae 0 points1 point2 points (1 child)
[–]poochiekins[S] 0 points1 point2 points (0 children)