4

Possible Duplicate:
“Least Astonishment” in Python: The Mutable Default Argument

I have something quite strange when I tried to write a class like below. On the line 3, I must put a new copy of the argument newdata to the self.data, otherwise, it seems that when I initiate a new class instance, the value of the previous one is still remembered by the class. see the example below and please pay attention to the only difference in the two versions of code on line 3.

class Pt(object):                                                               
    def __init__(self,newdata={}):                                              
        self.data=newdata.copy()                                                       
        if self.data == {}:                                                     
            self._taglist = []                                                  
        else:                                                                   
            self._taglist = self.data.keys()                                    
    def add_tag(self,tag=None):                                                 
        self.data[tag]={'x':[0,1,2,3,4]}                                        
        self._taglist.append(tag)                                               


In [49]: pt = Pt()

In [50]: pt.add_tag('b')

In [51]: pt.add_tag('a')

In [52]: pt.data
Out[52]: {'a': {'x': [0, 1, 2, 3, 4]}, 'b': {'x': [0, 1, 2, 3, 4]}}

In [53]: pt2 = Pt()

In [54]: pt2._taglist
Out[54]: []

class Pt(object):                                                               
    def __init__(self,newdata={}):                                              
        self.data=newdata                                                       
        if self.data == {}:                                                     
            self._taglist = []                                                  
        else:                                                                   
            self._taglist = self.data.keys()                                    
    def add_tag(self,tag=None):                                                 
        self.data[tag]={'x':[0,1,2,3,4]}                                        
        self._taglist.append(tag)                                               

In [56]: pt = Pt()

In [57]: pt.add_tag('a')

In [58]: pt.add_tag('b')

In [59]: pt._taglist
Out[59]: ['a', 'b']

In [60]: pt2 = Pt()

In [61]: pt2._taglist
Out[61]: ['a', 'b']

In [62]: pt2.data
Out[62]: {'a': {'x': [0, 1, 2, 3, 4]}, 'b': {'x': [0, 1, 2, 3, 4]}}

I guess the second case happens because: both newdata and self.data refer to the same object (but how could this happen, the value should be given from right to the left but not reverse), so when I use the "add_tag" method to update the self.data, the newdata is updated as well. Yet I think when I initiate a new instance by pt2 = Pt(), the newdata should use the default value ({}), can it still keeps the old value from pt1?

Community
  • 1
  • 1
wiswit
  • 5,599
  • 7
  • 30
  • 32

2 Answers2

4

The why and all is already explained very well in “Least Astonishment” in Python: The Mutable Default Argument. So here is just a quick help how to fix your problem.

As default parameter objects are kept, you should never use mutable objects as default parameter values. Instead, you should define a fixed value, usually None, as the default value for which then the default object is initialized. So your constructor would look like this:

def __init__(self, newdata=None):
    if newdata is None:
        newdata = {}
    # ...
Community
  • 1
  • 1
poke
  • 369,085
  • 72
  • 557
  • 602
  • 1
    I wouldn't say **never** use mutables as defaults. Sometimes, that's exactly the behavior you want. – Jerry B Oct 05 '13 at 07:04
  • @JerryB That seems somewhat unlikely, but if that’s really the case, you should use some other mechanism to access the shared object instead of using it as a default parameter, as that is obviously confusing people. – poke Oct 05 '13 at 15:08
  • Sure, it's bound to be rare, and any example I tried to give would necessarily be contrived. But rare is not the same as never. It's like when I was in college in the 80's, they kept hammering home "Never use goto!" But I have yet to deal with any language that **didn't** have a goto, just named something else and with built-in limitations/protections. – Jerry B Oct 05 '13 at 20:20
  • @JerryB Python has no goto. That is unless you call loops and conditional statements gotos named something else – which would be ridiculous as jumps is how the hardware works, so you cannot not use them. – poke Oct 05 '13 at 20:31
  • That's exactly it: **every** control structure is in fact a form of goto. And that is indeed the point: you can't **not** use some form of jumping to accomplish any non-trivial algorithm. Things like if and for just force you to do so in a way that people reading your code can understand easier. – Jerry B Oct 07 '13 at 02:05
  • @JerryB And your point is? As long as there are better, clearer, simpler ways to express what you are doing, you shouldn’t use the low level stuff. – poke Oct 07 '13 at 10:13
  • Sometimes goto **is** the best, clearest, simplest way. – Jerry B Oct 08 '13 at 17:59
1

Your issue is that you copy the dictionary, you create a new dictionary with the same values. This means that the lists in your dictionaries are the same, so when you modify them, they are modified in all your dictionaries.

What you want to do is a copy.deepcopy() - this will not just copy the dictionaries, copy the lists as well.

Gareth Latty
  • 86,389
  • 17
  • 178
  • 183