how to convert a multiline string to an anonymous function?

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
 
D

Diez B. Roggisch

Danny said:
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...

The "stupid" thing is that you can pass your own dictionary as globals
to exec. Then you can get a reference to the function under the name "f"
in the globals, and store that under whatever name you need.

Beware of recursion though! If that happens, you need to create unique
names for your functions, but as you know these beforehand I don't see
any problem with that - just enumerate them, like f1, f2, f3....

Diez
 
M

Matimus

Danny Shevitz schrieb:


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...

The "stupid" thing is that you can pass your own dictionary as globals
to exec. Then you can get a reference to the function under the name "f"
in the globals, and store that under whatever name you need.

Beware of recursion though! If that happens, you need to create unique
names for your functions, but as you know these beforehand I don't see
any problem with that - just enumerate them, like f1, f2, f3....

Diez

In other words:
.... def f(state):
.... print state
.... return True
.... '''Traceback (most recent call last):
File said:
state
True


Matt
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top