modifying static (local) method variables

Steve Holden steve at holdenweb.com
Fri Nov 12 11:57:48 EST 2004


t scytale wrote:
> i am using a mutable default paramater to simulate a static method variable.
> i notice that 
> - assigning to the var inside the method causes it to return to the
> default value at each call.
> - appending to the var gives the expected behaviour (the value is
> preserved to the next call
> 
> why is this?
> 

Because an assignment to an argument inside the method merely rebinds 
the local variable for the remainder of that execution. Mutating the 
referenced object ensures that the changed value is available to further 
calls.

regards
  Steve
-- 
http://www.holdenweb.com
http://pydish.holdenweb.com
Holden Web LLC +1 800 494 3119



More information about the Python-list mailing list