Willem Broekema | 8 Oct 19:50

effect of "exec" on local scope

The issue came up while trying to get some Sympy code running on CLPython.

class C:
 exec "a = 3"
 print locals()

1. Is it guaranteed that class C gets an attribute "a", i.e. that the
locals printed include {'a': 3}?
2. It it (also) guaranteed if it were in a function scope?

The complete syntax of exec is:
 exec CODE in Y, Z
where Y, Z are optional.

The documentation of "exec" says "if the optional parts are
omitted,the code is executed in the current scope." There are at least
two different interpretations:

 a. The code is executed in the current class scope, so the assignment
must have an effect on the class scope.

 b. The scope defaults to the local scope, by which is meant the
mapping returned by locals(), and of locals() the documentation says
that changes made to it may not influence the interpreter. (The
documentation of exec suggests using globals() and locals() as
arguments to exec, which seems hint at this interpretation.)

The relevant documentation:
 exec: http://docs.python.org/reference/simple_stmts.html#grammar-token-exec_stmt
 locals: http://docs.python.org/library/functions.html#locals
(Continue reading)

Terry Reedy | 8 Oct 21:17

Re: effect of "exec" on local scope

Willem Broekema wrote:
> The issue came up while trying to get some Sympy code running on CLPython.
> 
> class C:
>  exec "a = 3"
>  print locals()
> 
> 1. Is it guaranteed that class C gets an attribute "a", i.e. that the
> locals printed include {'a': 3}?
> 2. It it (also) guaranteed if it were in a function scope?
> 
> The complete syntax of exec is:
>  exec CODE in Y, Z
> where Y, Z are optional.
> 
> The documentation of "exec" says "if the optional parts are
> omitted,the code is executed in the current scope." There are at least
> two different interpretations:
> 
>  a. The code is executed in the current class scope, so the assignment
> must have an effect on the class scope.
> 
>  b. The scope defaults to the local scope, by which is meant the
> mapping returned by locals(), and of locals() the documentation says
> that changes made to it may not influence the interpreter. (The
> documentation of exec suggests using globals() and locals() as
> arguments to exec, which seems hint at this interpretation.)
> 
> The relevant documentation:
>  exec: http://docs.python.org/reference/simple_stmts.html#grammar-token-exec_stmt
(Continue reading)

Ondrej Certik | 9 Oct 01:40

Re: effect of "exec" on local scope

Hi Terry,

On Wed, Oct 8, 2008 at 9:17 PM, Terry Reedy <tjreedy <at> udel.edu> wrote:
> Willem Broekema wrote:
>>
>> The issue came up while trying to get some Sympy code running on CLPython.
>>
>> class C:
>>  exec "a = 3"
>>  print locals()
>>
>> 1. Is it guaranteed that class C gets an attribute "a", i.e. that the
>> locals printed include {'a': 3}?
>> 2. It it (also) guaranteed if it were in a function scope?
>>
>> The complete syntax of exec is:
>>  exec CODE in Y, Z
>> where Y, Z are optional.
>>
>> The documentation of "exec" says "if the optional parts are
>> omitted,the code is executed in the current scope." There are at least
>> two different interpretations:
>>
>>  a. The code is executed in the current class scope, so the assignment
>> must have an effect on the class scope.
>>
>>  b. The scope defaults to the local scope, by which is meant the
>> mapping returned by locals(), and of locals() the documentation says
>> that changes made to it may not influence the interpreter. (The
>> documentation of exec suggests using globals() and locals() as
(Continue reading)

Guido van Rossum | 9 Oct 03:02
Favicon

Re: effect of "exec" on local scope

Well, I don't recall what CLPython is, but I believe it is broken and
that code should work -- there are (or used to be) examples of using
exec to populate classes in the standard library so while it may look
dodgy it really is exected to work...

On Wed, Oct 8, 2008 at 4:40 PM, Ondrej Certik <ondrej <at> certik.cz> wrote:
> Hi Terry,
>
> On Wed, Oct 8, 2008 at 9:17 PM, Terry Reedy <tjreedy <at> udel.edu> wrote:
>> Willem Broekema wrote:
>>>
>>> The issue came up while trying to get some Sympy code running on CLPython.
>>>
>>> class C:
>>>  exec "a = 3"
>>>  print locals()
>>>
>>> 1. Is it guaranteed that class C gets an attribute "a", i.e. that the
>>> locals printed include {'a': 3}?
>>> 2. It it (also) guaranteed if it were in a function scope?
>>>
>>> The complete syntax of exec is:
>>>  exec CODE in Y, Z
>>> where Y, Z are optional.
>>>
>>> The documentation of "exec" says "if the optional parts are
>>> omitted,the code is executed in the current scope." There are at least
>>> two different interpretations:
>>>
>>>  a. The code is executed in the current class scope, so the assignment
(Continue reading)

Nick Coghlan | 9 Oct 13:27

Re: effect of "exec" on local scope

Guido van Rossum wrote:
> Well, I don't recall what CLPython is, but I believe it is broken and
> that code should work -- there are (or used to be) examples of using
> exec to populate classes in the standard library so while it may look
> dodgy it really is exected to work...

I think this behaviour (modifying locals() at class scope) was actually
implicitly blessed in PEP 3115, even though that PEP doesn't explicitly
state locals() in a class body is required to grant access to the
namespace returned by __prepare__().

Perhaps the time is right for the locals() documentation to be more
explicit regarding when modifying its contents will affect the current
namespace?
- yes at module scope (effectively the same as globals())
- yes at class scope
- maybe (but typically not) at function scope

Cheers,
Nick.

--

-- 
Nick Coghlan   |   ncoghlan <at> gmail.com   |   Brisbane, Australia
---------------------------------------------------------------
            http://www.boredomandlaziness.org
_______________________________________________
Python-Dev mailing list
Python-Dev <at> python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/python-python-dev%40m.gmane.org
(Continue reading)

Terry Reedy | 9 Oct 05:18

Re: effect of "exec" on local scope

Ondrej Certik wrote:

> Which works in CPython but fails in CLPython. From your answer it
> seems to me that this code is not nice and we should not use it and
> should rather use something like:
> 
> class Basic(AssumeMeths):
>   ...
> 
> for k in AssumeMeths._assume_defined:
>   setattr(Basic, 'is_%s' % k, property(make__get_assumption('Basic', '%s' % k)))
> 
> which should work on all platforms. What do you think?

That is what setattr is for.  Many consider exec a last resort.  I think 
any further discussion should move to the general python list or c.l.p 
since this is not a develop-core-python issue.

_______________________________________________
Python-Dev mailing list
Python-Dev <at> python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/python-python-dev%40m.gmane.org


Gmane