Is it possible to define a function to behave as follows?
text = "def x(a):\treturn a+1"
f = ??(text)
f(1)
>> 2
You can use exec()
text = "def x(a):\treturn a+1"
exec(text)
print x(5) # gives 6
f(5) to give 6, when you don't know the contents of text and don't know that the function is currently called x, and so you can't just do f = xtext = "lambda a: a + 1"
f = eval(text)
f(1) # 2
here is another solution:
text = "def x(a):\treturn a+1"
f = {}
exec text in f
f['x'](1)
>> 2
exec("print 'hello'")or any otherdefcan't be on the same line as other statements", then you can instead use lambdas:print (lambda a: a+1)(1)