By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,107 Members | 1,253 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,107 IT Pros & Developers. It's quick & easy.

Dictionary, iterate & update objects

P: n/a
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

There has to be a smarter way? Thankful for any piece of
enlightenment...

regards, Henning

Dec 16 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a

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

Dec 16 '06 #2

P: n/a
Hi and thanx!

Caleb Hattingh wrote:
>
>temp=d[12345]
temp.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?
Yes.
[... the temp object is by ref, and any manipulation of the temp object
is to the object itself..? It really simplifies my first attempt]
If you are this Henning Jansen:
http://www.henning-jansen.com/
Yes, this is me :-/ and I have a little Ruby experience. And I will
create a sample snippet the next time I pop a question. Thank you for a
clear and helpful response.

regards, Henning

Dec 16 '06 #3

P: n/a
jansenh a écrit :
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.
class MyObj(object):
def __init__(self, foo):
self.foo = foo

objs = {
'foo': MyObj('foo'),
'bar', MyObj('bar'),
}

objs['foo'].foo = 42

for key, obj in objs:
print "%s : %s" % (key, obj.foo)
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
My my my...
There has to be a smarter way?
Indeed. Usually, with Python, "smarter" ="simplest"

HTH
Dec 18 '06 #4

P: n/a

Dennis Lee Bieber wrote:
On 16 Dec 2006 14:49:19 -0800, "jansenh" <he************@gmail.com>
declaimed the following in comp.lang.python:
Yes.
[... the temp object is by ref, and any manipulation of the temp object
is to the object itself..? It really simplifies my first attempt]
For all practical purposes, everything in Python is "by ref" and the
difference is whether the object itself is mutable (dictionaries, lists,
class instances, modules) or immutable (strings, tuples, numbers). A
mutable object is basically something that contains references to other
objects
Not at all. It's not even generally true of Python. Mutability is
orthogonal to containership.

arrays, files, cStringIO objects, mmap objects, thread locks, some
iterator types, etc., are mutable but do not contain Python objects
(__dict__ excepted).

tuples, frozensets, instancemethods, are immutable but do contain
Python objects.
Carl Banks

Dec 18 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.