[Python-Dev] anonymous blocks

James Y Knight foom at fuhm.net
Wed Apr 20 16:29:48 CEST 2005


On Apr 20, 2005, at 5:43 AM, Paul Moore wrote:
>>> and the substitution of
>>>
>>> @EXPR:
>>>    CODE
>>>
>>> would become something like
>>>
>>> def __block():
>>>    CODE
>>> EXPR(__block)
>
> The question of whether assignments within CODE are executed within a
> new namespace, as this implies, or in the surrounding namespace,
> remains open. I can see both as reasonable (new namespace = easier to
> describe/understand, more in line with decorators, probably far easier
> to implement; surrounding namespace = probably more
> useful/practical...)

If it was possible to assign to a variable to a variable bound outside 
your function, but still in your lexical scope, I think it would fix 
this issue. That's always something I've thought should be possible, 
anyways. I propose to make it possible via a declaration similar to 
'global'.

E.g. (stupid example, but it demonstrates the syntax):
def f():
   count = 0
   def addCount():
     lexical count
     count += 1
   assert count == 0
   addCount()
   assert count == 1

Then, there's two choices for the block decorator: either automatically 
mark all variable names in the immediately surrounding scope "lexical", 
or don't. Both of those choices are still consistent with the block 
just being a "normal function", which I think is an important 
attribute.

James



More information about the Python-Dev mailing list