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

Ethan Furman ethan at stoneleaf.us
Wed Sep 28 03:26:53 CEST 2011

Terry Reedy wrote:
> 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.

Simplicity is good.

I don't remember who first suggested it, and I don't remember any 
discussion on it, but what if decorator functions grew a __prepare__ 
method similar to metaclasses?

If the method exists it is called /before/ the function is evaluated, it 
returns a namespace that is then used as the basis for the function's 
(the interpreter can take everything it finds in there and add it as 
closures to the function), and folks can call it whatever they want. ;)

If the method does not exist, nothing is different from what we have now.

Seeing-if-we-can-have-more-exploding-heads'ly yours,

More information about the Python-ideas mailing list