Returning a value from exec or a better solution
Jack Trades
jacktradespublic at gmail.com
Mon Aug 29 19:39:36 EDT 2011
On Mon, Aug 29, 2011 at 5:50 PM, Arnaud Delobelle <arnodel at gmail.com> wrote:
>
> Hi Jack,
>
> Here is a possible solution for your problem (Python 3):
>
>
> >>> class CapturingDict(dict):
> ... def __setitem__(self, key, val):
> ... self.key, self.val = key, val
> ... dict.__setitem__(self, key, val)
> ...
> >>> c = CapturingDict()
> >>> exec("def myfunction(x): return 1", c)
> >>> c.key
> 'myfunction'
> >>> c.val
> <function myfunction at 0x100634d10>
>
> HTH,
>
> --
> Arnaud
>
That's brilliant and works flawlessly. Thank you very much!
--
Nick Zarczynski <http://www.rentageekit.com>
Pointless Programming Blog <http://pointlessprogramming.wordpress.com>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110829/4bffe7dd/attachment-0001.html>
More information about the Python-list
mailing list