Jack Trades wrote in
news:CAG5udOg=GtFGPmTB=1ojnvnrpdyucxdokn1wjqmomv9gx0+...@mail.gmail.com
in gmane.comp.python.general: 

> ... I wanted to allow the user to manually return the
> function from the string, like this:
> 
> a = exec("""
> def double(x):
>   return x * 2
> double
> """)
> 
> However it seems that exec does not return a value as it produces a
> SyntaxError whenever I try to assign it.

def test():
  src = (
      "def double(x):"
      "  return x * 2"
    )
  globals  = {}
  exec( src, globals )
  return globals[ "double" ]
  
print( test() )

The a bove works on 2.7 (I tested it) on earlier versions you may need 
to use:

        exec src in globals 

Rob.

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to