how to convert a multiline string to an anonymous function?

Diez B. Roggisch deets at nospam.web.de
Tue Apr 29 18:39:52 EDT 2008


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



More information about the Python-list mailing list