When do default parameters get their values set?

Duncan Booth duncan.booth at invalid.invalid
Tue Dec 9 16:18:03 EST 2014


"Dave Angel" <dave.angel at 1:249/999.remove-cdt-this> wrote:

> On 12/08/2014 05:10 PM, bSneddon wrote:
>> I ran into an issue setting variables from a GUI module that imports
>> a back 
> end module.  My approach was wrong obviously but what is the best way
> to set values in a back end module.
>>
> 
> To answer the subject line, the default parameter(s) are evaluated
> when the function is compiled, and then stored with the function.

The function is compiled when the module is compiled. At latest that is 
when the module is imported (though in most cases it was probably compiled 
the first time you ran the code and isn't recompiled unless the source code 
changes).

The default parameters are actually evaluated when the 'def' statement is 
executed and the function object is created from the default arguments and 
the previously compiled code block.

-- 
Duncan Booth http://kupuguy.blogspot.com



More information about the Python-list mailing list