Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old December 16th, 2006, 09:05 PM
jansenh
Guest
 
Posts: n/a
Default Dictionary, iterate & update objects

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

  #2  
Old December 16th, 2006, 09:45 PM
Caleb Hattingh
Guest
 
Posts: n/a
Default Re: Dictionary, iterate & update objects


jansenh wrote:
Quote:
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:
Quote:
Quote:
Quote:
>>d = {}
>>class o(object):
.... def __init__(self):
.... self.x = 0
....
Quote:
Quote:
Quote:
>>i = o()
>>d[12345]=i
>>temp=d[12345]
>>temp.x = 5
>>d[12345].x
5
Quote:
Quote:
Quote:
>>i.x
5
Quote:
Quote:
Quote:
>>>
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

  #3  
Old December 16th, 2006, 10:55 PM
jansenh
Guest
 
Posts: n/a
Default Re: Dictionary, iterate & update objects

Hi and thanx!

Caleb Hattingh wrote:
Quote:
>
Quote:
Quote:
>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]


Quote:
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

  #4  
Old December 18th, 2006, 11:05 AM
Bruno Desthuilliers
Guest
 
Posts: n/a
Default Re: Dictionary, iterate & update objects

jansenh a écrit :
Quote:
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)
Quote:
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...
Quote:
There has to be a smarter way?
Indeed. Usually, with Python, "smarter" ="simplest"

HTH
  #5  
Old December 18th, 2006, 06:45 PM
Carl Banks
Guest
 
Posts: n/a
Default Re: Dictionary, iterate & update objects


Dennis Lee Bieber wrote:
Quote:
On 16 Dec 2006 14:49:19 -0800, "jansenh" <henning.jansen@gmail.com>
declaimed the following in comp.lang.python:
>
Quote:
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

 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles