Re: how to convert a multiline string to an anonymous function?
- From: Scott David Daniels <Scott.Daniels@xxxxxxx>
- Date: Wed, 30 Apr 2008 06:00:36 -0700
Matimus wrote:
On Apr 29, 3:39 pm, "Diez B. Roggisch" <de...@xxxxxxxxxxxxx> wrote:Or, you can even more simply do:Danny Shevitz schrieb:...Simple question here: ...The "stupid" thing is that you can pass your own dictionary as globals
str = '''
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....
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....
Diez
text = '''
def f(state):
print state
return True
'''
lcl = {}
exec text in globals(), lcl
and lcl will be a dictionary with a single item: {'f' : <function>}
so you could return lcl.values()[0]
-Scott David Daniels
Scott.Daniels@xxxxxxx
.
- Follow-Ups:
- Re: how to convert a multiline string to an anonymous function?
- From: Danny Shevitz
- Re: how to convert a multiline string to an anonymous function?
- References:
- how to convert a multiline string to an anonymous function?
- From: Danny Shevitz
- Re: how to convert a multiline string to an anonymous function?
- From: Diez B. Roggisch
- Re: how to convert a multiline string to an anonymous function?
- From: Matimus
- how to convert a multiline string to an anonymous function?
- Prev by Date: Re: xml.dom.minidom weirdness: bug?
- Next by Date: relative import broken?
- Previous by thread: Re: how to convert a multiline string to an anonymous function?
- Next by thread: Re: how to convert a multiline string to an anonymous function?
- Index(es):
Relevant Pages
|