Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #12429 > unrolled thread
| Started by | Rob Williscroft <rtw@rtw.me.uk> |
|---|---|
| First post | 2011-08-30 07:28 +0000 |
| Last post | 2011-08-30 07:28 +0000 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: Returning a value from exec or a better solution Rob Williscroft <rtw@rtw.me.uk> - 2011-08-30 07:28 +0000
| From | Rob Williscroft <rtw@rtw.me.uk> |
|---|---|
| Date | 2011-08-30 07:28 +0000 |
| Subject | Re: Returning a value from exec or a better solution |
| Message-ID | <mailman.564.1314689337.27778.python-list@python.org> |
Jack Trades wrote in
news:CAG5udOh1+oE4g9Frjp3pucbHUtWcN34KK35a-Xs2YqkZH9X5=w@mail.gmail.com
in gmane.comp.python.general:
>> def test():
>> src = (
>> "def double(x):"
>> " return x * 2"
>> )
>> globals = {}
>> exec( src, globals )
>> return globals[ "double" ]
>>
>> print( test() )
>>
>
> I looked into doing it that way but it still requires that the user
> use a specific name for the function they are defining. The docs on
> exec say that an implementation may populate globals or locals with
> whatever they want so that also rules out doing a simple "for item in
> globals", as there may be more than just the one function in there
> (though I suppose I may be able to work around that).
>
>
Why not just get the name from the user, or use a regular expression
to extract the first (or last, maybe) definition from the source string.
Rob.
Back to top | Article view | comp.lang.python
csiph-web