jansenh wrote: > hi comp.lang.python. > > I need some newbe advice on idiomatic use of Python dictionaries. > > I have service with a dictionary which holds a bunch of objects as > values, and an ID as key to each object. Then I want to change an > objects state based on its key. The way I am doing this now is by using > 'fromkeys' and copying my object over in a temporary dictionary, then > manipulating the object, and then I do an 'update' back to the main > dictionary.. :-0
It would be easier to help if you post a short snippet of code that demonstrates the essence of the issue, but I made an attempt at coding what you describe: >>> d = {} >>> class o(object): ... def __init__(self): ... self.x = 0 ... >>> i = o() >>> d[12345]=i >>> temp=d[12345] >>> temp.x = 5 >>> d[12345].x 5 >>> i.x 5 >>> After I assign the object to the dict with ID=12345, I can easily get the object by its key and manipulate its state. The last 4 lines show that the state changed for all the active references to the created object. Is this what you wanted? If you are this Henning Jansen: http://www.henning-jansen.com/ you may get a lot much more accurate help from others in this group by mentioning that you have some Ruby experience. That way, the guys in here who also know Ruby (not me :) can quickly point out the differences in behaviour for your specific question. Regards Caleb -- http://mail.python.org/mailman/listinfo/python-list