Steven D'Aprano wrote: > On Wednesday 09 March 2016 16:27, Veek. M wrote: > >> What is the return value of `exec`? Would that object be then used to >> iterate the sequence in 'a'? I'm reading this: >> https://www.python.org/download/releases/2.2.3/descrintro/ > > > exec is a statement, not a function, so it doesn't have a return > value. > > Are you referring to this line of code? > > exec "x = 3; print x" in a > > > That doesn't return a value. The "in a" part tells exec which > namespace to execute the code in. It doesn't mean "test if the result > is found inside sequence a". > > py> namespace = {'__builtins__': None} > py> exec "x = 3" in namespace > py> namespace > {'__builtins__': None, 'x': 3} > > > If you leave the "in namespace" part out, then exec will use the > current namespace, and x will become a local variable. > > > What happens if you don't put the special __builtins__ key into the > namespace? Python adds it for you: > > > py> mydict = {} > py> exec "foo = 99.99" in mydict > py> mydict.keys() > ['__builtins__', 'foo'] > > > > What's inside __builtins__? Every single built-in function and class: > > py> mydict['__builtins__'] > {'bytearray': <type 'bytearray'>, 'IndexError': <type > 'exceptions.IndexError'>, 'all': <built-in function all>, > > ... dozens more entries ... > > 'OverflowError': <type 'exceptions.OverflowError'>} > > > > ah, okay - i'm familiar with the py3 syntax where you do: eval('whatever stmt', globals={}, locals={}) I suppose this: exec " " in NS; syntax is strictly py2?
Many thanks :) -- https://mail.python.org/mailman/listinfo/python-list