A very popular and common question. Detailed explanation: 
http://stackoverflow.com/questions/1132941/least-astonishment-in-python-the-mutable-default-argument



On Aug 10, 2012, at 4:17 AM, Adam Mechtley <[email protected]> wrote:

> Not a bug, and not restricted to class methods. Same thing happens of you 
> define a list in your default parameter list and mutate it in your method 
> body. The issue is you have a reference to the same mutable object (the one 
> defined in te paramter list) when you're not passing an explicit value. If 
> you need a default value for a mutable object, always set it inside your 
> method body, not in the parameter list.
> 
> On Friday, August 10, 2012, Jay Goodman wrote:
> I found some 'wierdness' with classmethods today (in 2.5).  I have a 
> classmethod with kwargs, and I noticed that the kwarg was, somehow, taking on 
> a global scope.
> 
> example:
> ---------        
>     @classmethod        
>     def classMethodName (cls, argName, kwargName={}):
>          doStuff
> --------
> 
> 1st Call--> classMethodName(argName) # kwarg is not passed in
>   kwarg is empty
>   kwarg (dictionary) ends up getting filled with data
>   kwarg is NOT cleared (clearing it is a workaround, though)
> 
> 2nd Call--> classMethodName(argName) #same as the first
>   kwarg is NOT EMPTY--it contains the data from the 1st call
> 
> So somehow, the kwarg is getting global scope?  Is this how classmethods are 
> designed to work?  At first I thought I screwed up and this variable was 
> defined globally, but that is not the case.  Also, this issue can be worked 
> around by either clearing the dictionary or using kwargs.  I was just looking 
> for a better understanding as to why it was happening this way?  
> Any takers?
> thanks!
> 
> 
> 
> -- 
> view archives: http://groups.google.com/group/python_inside_maya
> change your subscription settings: 
> http://groups.google.com/group/python_inside_maya/subscribe
> -- 
> view archives: http://groups.google.com/group/python_inside_maya
> change your subscription settings: 
> http://groups.google.com/group/python_inside_maya/subscribe

-- 
view archives: http://groups.google.com/group/python_inside_maya
change your subscription settings: 
http://groups.google.com/group/python_inside_maya/subscribe

Reply via email to