D
Danny Shevitz
Simple question here:
I have a multiline string representing the body of a function. I have control
over the string, so I can use either of the following:
str = '''
print state
return True
'''
str = '''
def f(state):
print state
return True
'''
and I want to convert this into the function:
def f(state):
print state
return True
but return an anonmyous version of it, a la 'return f' so I can assign it
independently. The body is multiline so lambda doesn't work.
I sort of need something like:
def function_constructor(str):
f = eval(str) # What should this be
return f
functions = {}
for node in nodes:
function[node] = function_constructor(node.text)
I'm getting stuck because 'def' doesn't seem to work in an eval function,
and exec actually modifies the namespace, so I run into collisions if I use
the function more than once.
I know I'm missing something stupid here, but I'm stuck just the same...
thanks,
Danny
I have a multiline string representing the body of a function. I have control
over the string, so I can use either of the following:
str = '''
print state
return True
'''
str = '''
def f(state):
print state
return True
'''
and I want to convert this into the function:
def f(state):
print state
return True
but return an anonmyous version of it, a la 'return f' so I can assign it
independently. The body is multiline so lambda doesn't work.
I sort of need something like:
def function_constructor(str):
f = eval(str) # What should this be
return f
functions = {}
for node in nodes:
function[node] = function_constructor(node.text)
I'm getting stuck because 'def' doesn't seem to work in an eval function,
and exec actually modifies the namespace, so I run into collisions if I use
the function more than once.
I know I'm missing something stupid here, but I'm stuck just the same...
thanks,
Danny