[Python-ideas] Before and after the colon in funciton defs.

Terry Reedy tjreedy at udel.edu
Fri Sep 23 23:18:13 CEST 2011


On 9/23/2011 7:26 AM, H. Krishnan wrote:
> Is there a restriction that nonlocal can be used only inside nested
> functions.

 >>> a=3
 >>> def f(): nonlocal a; return a
SyntaxError: no binding for nonlocal 'a' found

Yes, althought the current 3.2.2 manual does not make that clear enough. 
I believe there is a tracker issue to clarify this.
>
> Also, would it make sense/be possible to execute nested class/function
> definitions at function definition time.

No. The body of a function is executed when the function is called. 
However, it is already the case that code objects for nested functions 
*are* compiled just once. So just a nested function object has to be 
assembled with each call. Default args and cell vars are part of each 
new function object.

-- 
Terry Jan Reedy




More information about the Python-ideas mailing list