I had thought I was developing a clue regarding objects in Python, but I ran across an oddity today that I didn't expect. It had seemed to me that once an instance was created, it would have a separate data pool from any other instance of the same object. It would share methods, of course, but the data assigned to it would be totally separate. After running into peculiar errors I now see how this is not totally true, but I would like to understand why not. I think I'm missing something kind of fundamental here.
The following is my toy example: In [1]: class test(object): ...: testlist = [] ...: In [2]: c1 = test() In [3]: c1.testlist.append(0) In [4]: c2 = test() In [5]: c2.testlist Out[5]: [0] In this example, I create two instances of the object test, assigned data to c1.testlist and it shows up in the other instance, c2. Typically, I have been assigning values within an object from being passed in via __init__, but it seemed like it wasn't really necessary for this particular kind of object. In my next example, I show the creation of testlist via an __init__ and all goes as I expected. In [10]: class test(object): ....: def __init__(self): ....: self.testlist = [] ....: In [11]: c1 = test() In [12]: c2 = test() In [13]: c1.testlist.append(0) In [14]: c2.testlist Out[14]: [] So, I can see the error of my ways. I can also see that this behavior gives me an opportunity to globally change a value in all of the object instances, if I ever had to do something like that. I just don't have a clue as to why objects were designed this way. Can anyone point me in the right direction? Thanks ds _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor