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

Jeff Shannon jeff at ccvcorp.com
Thu Jan 13 12:50:39 EST 2005


Nick Coghlan wrote:

> 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

Personally, I think that the fact that the bridging statement is 
executed *after* the inner code block guarantees that results will be 
surprising.  The fact that it effectively introduces *two* new scopes 
just makes matters worse.

It also seems to me that one could do this using a nested function def 
with about the same results.  You wouldn't have a bridging scope with 
both sets of names as locals, but your nested function would have 
access to the outer namespace via normal nested scopes, so I'm really 
not seeing what the gain is...

(Then again, I haven't been following the whole using/where thread, 
because I don't have that much free time and the initial postings 
failed to convince me that there was any real point...)

Jeff Shannon
Technician/Programmer
Credit International



> 
> 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.
> 




More information about the Python-list mailing list