<div class="gmail_quote">On Mon, Aug 29, 2011 at 12:30 PM, Rob Williscroft <span dir="ltr"><<a href="mailto:rtw@rtw.me.uk">rtw@rtw.me.uk</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
Jack Trades wrote in<br>
> ... I wanted to allow the user to manually return the<br>
<div class="im">> function from the string, like this:<br>
><br>
> a = exec("""<br>
> def double(x):<br>
>   return x * 2<br>
> double<br>
> """)<br>
><br>
> However it seems that exec does not return a value as it produces a<br>
> SyntaxError whenever I try to assign it.<br>
<br>
</div>def test():<br>
  src = (<br>
<div class="im">      "def double(x):"<br>
      "  return x * 2"<br>
</div>    )<br>
  globals  = {}<br>
  exec( src, globals )<br>
  return globals[ "double" ]<br>
<br>
print( test() )<br></blockquote><div><br>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).<br>
<br clear="all"></div></div>-- <br>Nick Zarczynski<br><a href="http://pointlessprogramming.wordpress.com" target="_blank">Pointless Programming Blog</a><br><br>