On Thu, Sep 16, 2010 at 10:13 AM, Jason Swails <jason.swa...@gmail.com> wrote: > Hello everyone, > > I'm encountering an issue in one of my Python classes that makes extensive > use of dictionaries. I was under the impression that each time an object > was instantiated, all of its variables were created in a new section of > memory, so that if you change the value of the variable in one instance, it > left that variable's value in another instance alone. In the object that I > wrote, I have 3 different dictionaries: parm_data, pointers, and formats, > all defined in the same place. When I load 2 instances of this object, > parm_data and formats each take on different values between the two objects > (as they should), but for some reason pointers does not. I've seen this > problem with python2.6.4 and 2.6.1 (and I believe earlier versions as well, > but I'm not sure). I've attached a tarball with the relevant code and a > sample script that shows what I mean. > > If anyone can tell me why the dictionary from 2 different objects are > exactly the same for pointers, but are different for, e.g. parm_data and > formats, that would be greatly appreciated.
In short (without creating a huge thread of unnecessary chatter), if you reference an object in 2 different dictionaries, the values (dictionary values) will be identical. The following example might help you understand this. >>> class Foo(object): ... def __init__(self, data=None): ... self.data = {"params": data} ... >>> a = b = object() >>> id(a), id(b) (3075279112L, 3075279112L) >>> foo = Foo(a) >>> bar = Foo(b) >>> foo.data, bar.data ({'params': <object object at 0xb74d0908>}, {'params': <object object at 0xb74d0908>}) >>> id(foo.data["params"]), id(bar.data["params"]) (3075279112L, 3075279112L) >>> cheers James -- -- James Mills -- -- "Problems are solved by method" -- http://mail.python.org/mailman/listinfo/python-list