[Python-ideas] Tweaking closures and lexical scoping to include the function being defined

Terry Reedy tjreedy at udel.edu
Wed Sep 28 03:17:16 CEST 2011

On 9/27/2011 6:32 PM, Guido van Rossum wrote:
> On Tue, Sep 27, 2011 at 3:24 PM, Arnaud Delobelle<arnodel at gmail.com>  wrote:

>> I may be unduly worrying about this, but it feels to me that it
>> lessens the overall tidiness of the language.  I would much rather
>> there was a way to initialise these "own" variables outside the body
>> of the function.
> Yes, you have a point. Hiding side effects this way could be nasty,
> and this is actually the first point in favor of the default argument
> hack I've heard in a while. :-)

I have several times written on python-list that Python has a simple 
rule: header expressions (for default args) are evaluated one-time when 
the function is defined; body expressions are evaluated (perhaps) each 
time when the function is called. If people understand this and do not 
fight it, they are not surprised that mutating a once-defined default 
arg mutates it, nor that defining a function inside a loop magically 
causes define-time binding of names in the body.

I would hate for Python to lose this simplicity. I consider it one of 
its positive features.

Terry Jan Reedy

More information about the Python-ideas mailing list