Class Encapsulation Errors in Python 2.3.3

Jeff Shannon jeff at ccvcorp.com
Fri Nov 19 15:02:57 EST 2004


Duncan Booth wrote:

>Jeff Shannon wrote:
>
>  
>
>>The problem here is that default values in functions/methods are 
>>evaluated at *compile* time.
>>    
>>
>
>Actually, no. The default values in functions/methods are evaluated when 
>the def statement is *executed* not when anything is compiled. If you 
>execute the def multiple times the default values are recalculated each 
>time. 
>  
>

Hm, true, I misspoke.  Perhaps it's clearest to say that the default 
values are evaluated when the function/method object is *created*, not 
when it is called.

Functions are created when the def statement is executed; this is 
usually one of the first things that happens when a simple module/script 
is imported/run, but it can happen at other times as well.  Referring to 
the creation of function objects as compilation, as I did, is 
technically inaccurate (compilation being the conversion of text source 
code to bytecode, which is later executed).  I do know the difference, 
but tend to conflate them out of laziness in cases where the distinction 
has little practical difference.  (In this case, practical differences 
are usually only significant when function/class definitions are dynamic 
and/or some sort of deep magic is being employed, neither of which are 
typical for me to need...)  But I should indeed be more careful about 
how I state that, because while *I* know what I meant, it doesn't follow 
that someone else reading this and looking for guidance won't get bitten 
by my laziness.  :)

Jeff Shannon
Technician/Programmer
Credit International




More information about the Python-list mailing list