Dictionary, iterate & update objects

J

jansenh

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
 
C

Caleb Hattingh

jansenh said:
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:
.... 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
 
J

jansenh

Hi and thanx!

Caleb said:
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
 
D

Dennis Lee Bieber

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 -- tuples are a special case, as they are usable as hashable
keys in dictionaries; changing the contents would invalidate the hash
value.
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
B

Bruno Desthuilliers

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
 
C

Carl Banks

Dennis said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,008
Latest member
HaroldDark

Latest Threads

Top