Re: exec and locals

2014-02-27 Thread Steven D'Aprano
On Fri, 28 Feb 2014 00:29:35 +1300, Gregory Ewing wrote: > Steven D'Aprano wrote: >> On Thu, 27 Feb 2014 16:34:33 +1300, Gregory Ewing wrote: >> >>>Why not just use this version all the time? It should work in both 2.x >>>and 3.x. >> >> Because that's yucky. It's an aesthetic thing: when support

Re: exec and locals

2014-02-27 Thread Chris Angelico
On Thu, Feb 27, 2014 at 10:29 PM, Gregory Ewing wrote: > Steven D'Aprano wrote: >> >> On Thu, 27 Feb 2014 16:34:33 +1300, Gregory Ewing wrote: >> >>> Why not just use this version all the time? It should work in both 2.x >>> and 3.x. >> >> >> Because that's yucky. It's an aesthetic thing: when sup

Re: exec and locals

2014-02-27 Thread Gregory Ewing
Steven D'Aprano wrote: On Thu, 27 Feb 2014 16:34:33 +1300, Gregory Ewing wrote: Why not just use this version all the time? It should work in both 2.x and 3.x. Because that's yucky. It's an aesthetic thing: when supported, I want the Python interpreter to manage the context manager. More y

Re: exec and locals

2014-02-27 Thread Alister
On Thu, 27 Feb 2014 00:31:56 +, Steven D'Aprano wrote: > On Wed, 26 Feb 2014 14:00:59 +, Alister wrote: > >> On Wed, 26 Feb 2014 13:15:25 +, Steven D'Aprano wrote: >> >>> I have to dynamically generate some code inside a function using exec, >>> but I'm not sure if it is working by a

Re: exec and locals

2014-02-26 Thread Chris Angelico
On Thu, Feb 27, 2014 at 3:47 PM, Steven D'Aprano wrote: > Guys, I know that exec is kinda dangerous and newbies should be > discouraged from throwing every string they see at it, but this isn't my > second day Python programming, and it's not an accident that Python > supports the dynamic compilat

Re: exec and locals

2014-02-26 Thread Steven D'Aprano
On Wed, 26 Feb 2014 23:20:10 -0500, Dave Angel wrote: > Before I would use exec, I'd look hard at either generating a > source file to import, Yes, I went through the process of pulling out the code into a separate module, but that just made more complexity and was pretty nasty. If the func

Re: exec and locals

2014-02-26 Thread Steven D'Aprano
On Thu, 27 Feb 2014 16:34:33 +1300, Gregory Ewing wrote: > Steven D'Aprano wrote: >> except SyntaxError: >> def inner(): >> # manually operate the context manager call context manager >> __enter__ >> try: >> try: >>

Re: exec and locals

2014-02-26 Thread Dave Angel
Steven D'Aprano Wrote in message: > On Wed, 26 Feb 2014 14:46:39 +0100, Peter Otten wrote: > >> Steven D'Aprano wrote: >> >>> I have to dynamically generate some code inside a function using exec, >>> but I'm not sure if it is working by accident or if I can rely on it. >>> >> I eventually set

Re: exec and locals

2014-02-26 Thread Dan Sommers
On Thu, 27 Feb 2014 00:25:45 +, Steven D'Aprano wrote: > By the way, if anyone cares what my actual use-case is, I have a > function that needs to work under Python 2.4 through 3.4, and it uses > a with statement. With statements are not available in 2.4 (or 2.5, > unless you give a from __fut

Re: exec and locals

2014-02-26 Thread Gregory Ewing
Steven D'Aprano wrote: except SyntaxError: def inner(): # manually operate the context manager call context manager __enter__ try: try: return something except: # Yes, a bare except. Catch EVERYTH

Re: exec and locals

2014-02-26 Thread Steven D'Aprano
On Wed, 26 Feb 2014 14:00:59 +, Alister wrote: > On Wed, 26 Feb 2014 13:15:25 +, Steven D'Aprano wrote: > >> I have to dynamically generate some code inside a function using exec, >> but I'm not sure if it is working by accident or if I can rely on it. [...] > I have no idea but as exec

Re: exec and locals

2014-02-26 Thread Steven D'Aprano
On Wed, 26 Feb 2014 14:46:39 +0100, Peter Otten wrote: > Steven D'Aprano wrote: > >> I have to dynamically generate some code inside a function using exec, >> but I'm not sure if it is working by accident or if I can rely on it. >> >> Here is a trivial example: >> >> >> py> def spam(): >> ...

Re: exec and locals

2014-02-26 Thread Alister
On Wed, 26 Feb 2014 13:15:25 +, Steven D'Aprano wrote: > I have to dynamically generate some code inside a function using exec, > but I'm not sure if it is working by accident or if I can rely on it. > > Here is a trivial example: > > > py> def spam(): > ... exec( """x = 23""" ) > ...

Re: exec and locals

2014-02-26 Thread Peter Otten
Peter Otten wrote: > Steven D'Aprano wrote: > >> I have to dynamically generate some code inside a function using exec, >> but I'm not sure if it is working by accident or if I can rely on it. >> >> Here is a trivial example: >> >> >> py> def spam(): >> ... exec( """x = 23""" ) >> ...

Re: exec and locals

2014-02-26 Thread Peter Otten
Steven D'Aprano wrote: > I have to dynamically generate some code inside a function using exec, > but I'm not sure if it is working by accident or if I can rely on it. > > Here is a trivial example: > > > py> def spam(): > ... exec( """x = 23""" ) > ... return x > ... > py> spam() > 23

Re: exec and locals

2014-02-26 Thread Chris Angelico
On Thu, Feb 27, 2014 at 12:15 AM, Steven D'Aprano wrote: > py> def spam(): > ... exec( """x = 23""" ) > ... return x > ... > py> spam() > 23 > > > (My real example is more complex than this.) > > According to the documentation of exec, I don't think this should > actually work, and yet it

exec and locals

2014-02-26 Thread Steven D'Aprano
I have to dynamically generate some code inside a function using exec, but I'm not sure if it is working by accident or if I can rely on it. Here is a trivial example: py> def spam(): ... exec( """x = 23""" ) ... return x ... py> spam() 23 (My real example is more complex than this.)

Re: problem with exec and locals()

2008-07-11 Thread Gabriel Genellina
En Fri, 11 Jul 2008 03:51:39 -0300, Uwe Schmitt <[EMAIL PROTECTED]> escribi�: On 1 Jul., 15:15, Mel <[EMAIL PROTECTED]> wrote: rocksportrockerwrote: > the following code does not work until I ommit the "a=0" statement. >    def test(): >        exec "a=3" in locals() >        print a >    

Re: problem with exec and locals()

2008-07-11 Thread Peter Otten
Uwe Schmitt wrote: >> Apparently, exec in locals() knows nothing about slots (because locals() >> is the only dictionary in the universe where slots would be involved ? -- >> perhaps not, but close). >> >> Mel. > > Thanks for your answer. I wonder if this is a bug, or did I miss > something in th

Re: problem with exec and locals()

2008-07-10 Thread Uwe Schmitt
On 1 Jul., 15:15, Mel <[EMAIL PROTECTED]> wrote: > rocksportrockerwrote: > > > Hi, > > > the following code does not work until I ommit the "a=0" statement. > > >    def test(): > >        exec "a=3" in locals() > >        print a > >        a=0 > > >     test() > > > print raises: > >      Unbound

Re: problem with exec and locals()

2008-07-01 Thread Mel
rocksportrocker wrote: > > Hi, > > the following code does not work until I ommit the "a=0" statement. > > >def test(): >exec "a=3" in locals() >print a >a=0 > > test() > > print raises: > UnboundLocalError: local variable 'a' referenced before > assignme

problem with exec and locals()

2008-07-01 Thread rocksportrocker
Hi, the following code does not work until I ommit the "a=0" statement. def test(): exec "a=3" in locals() print a a=0 test() print raises: UnboundLocalError: local variable 'a' referenced before assignment Can anybody explain what is going wrong here ? Gree