storing references instead of copies in a dictionary

mk mrkafk at gmail.com
Thu Jul 17 10:51:02 EDT 2008


Uwe Schmitt wrote:
> Python stores references in dictionaries and does not copy ! (unless
> you explicitly use the copy module) !
> 
> In your case the entry in the dictionary is a reference to the same
> object which f1 references, that is the object at 0xb7f0ba04.
> 
> If you now say "f1=...:" then f1 references a new object
> at 0xb7f0b994, and the entry in your dictionary still references
> the "old" object at 0xb7f0ba04.

Erm, I theoretically knews that, I guess I suffered temporary insanity 
when I wrote "copies" of objects. To me it seems that Python actually 
stores _labels_ referencing _objects_. Here, the problem was that the 
label in the dictionary led to the "old" object.

> I do not know any method to automatically update your dictionary
> as there is no possibility to overload the assignement operator "=".
> But may be somebody can teach me a new trick :-)

Theoretically I could define a class inheriting from dictionary and 
define a property with a setter (and getter). But I would still have to 
update the attribute manually, so plain dictionary is just as good.




More information about the Python-list mailing list