Statement local namespaces summary (was Re: python3: 'where' keyword)

Nick Coghlan ncoghlan at iinet.net.au
Thu Jan 13 10:48:48 EST 2005


Nick Coghlan wrote:
> Semantics
> ---------
> The code::
> 
> <statement> with:
>    <suite>
> 
> translates to::
> 
> def unique_name():
>     <suite>
>     <statement>
> unique_name()

I've come to the conclusion that these semantics aren't what I would expect from 
the construct. Exactly what I would expect can't really be expressed in current 
Python due to the way local name bindings work. The main thing to consider is 
what one would expect the following to print:

def f():
     a = 1
     b = 2
     print 1, locals()
     print 3, locals() using:
         a = 2
         c = 3
         print 2, locals()
     print 4, locals()

I think the least suprising result would be:

1 {'a': 1, 'b': 2}         # Outer scope
2 {'a': 2, 'c': 3}         # Inner scope
3 {'a': 2, 'b': 2, 'c': 3} # Bridging scope
4 {'a': 1, 'b': 2}         # Outer scope

In that arrangement, the statement with a using clause is executed normally in 
the outer scope, but with the ability to see additional names in its local 
namespace. If this can be arranged, then name binding in the statement with the 
using clause will work as we want it to.

Anyway, I think further investigation of the idea is dependent on a closer look 
at the feasibility of actually implementing it. Given that it isn't as 
compatible with the existing nested scope structure as I first thought, I 
suspect it will be both tricky to implement, and hard to sell to the BDFL 
afterwards :(

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at email.com   |   Brisbane, Australia
---------------------------------------------------------------
             http://boredomandlaziness.skystorm.net



More information about the Python-list mailing list