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

Andrey Tatarinov elephantum at dezcom.mephi.ru
Sun Jan 16 07:04:53 EST 2005


Nick Coghlan wrote:
> # Anonymous functions
> use res:
>   def f(x):
>     d = {}
>     exec x in d
>     return d
> in:
>   res = [f(i) for i in executable]

as for me, I found construction "use <name>:" unobvious and confusing.
Also there is great possibility to forget some of variables names.

I think that syntax

<block>
where:
     <block>

is more obvious. (and we already have defined semantics for it)

we have two problems, that we try to solve
1) create method to nest scopes
2) create method to reverse execution order for better readability

"using:" solves both at once.
but your "use ... in ..." syntax shows, that you want to be able to 
solve 1) independently i.e. create nested scope without reversing 
execution order.

so, I can suggest one more keyword "do:", which will create nested 
scope, just as "def f(): ... ; f()" do (and that could be just syntaxic 
sugar for it.

so "use ... in ..." would look the following way:

do:
     res = [f(i) for i in executable]
     #some more equations here
     using:
         def f(x):
         d = {}
         exec x in d
         return d

that seems good for me. of course if you want to return something from 
the nest scope you must show that variable is from parent scope.

// while writing that I realized that it's too complex to be implemented 
in python in that way. consider it as some type of brainstorming.



More information about the Python-list mailing list