473,324 Members | 2,166 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

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

Dec 16 '06 #1
4 3531

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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Ling Lee | last post by:
Hello. I'm trying to write a small program that lets you put in a number as an integer and then it tells you the textuel representation of the number. Like if your input is 42, it will say...
4
by: Julian Yap | last post by:
Hi all, I'm trying to get some ideas on the best way to do this. In this particular coding snippet, I was thinking of creating a dictionary of file objects and file names. These would be...
10
by: dcrespo | last post by:
Hi all, How can I replace all None values with the string 'Null' in a dictionary? For example: convert this: a = {'item1': 45, 'item2': None} into this:
28
by: robert | last post by:
In very rare cases a program crashes (hard to reproduce) : * several threads work on an object tree with dict's etc. in it. Items are added, deleted, iteration over .keys() ... ). The threads are...
3
by: Mark Rae | last post by:
Hi, Is it possible to use a Dictionary<int, stringor even Dictionary<string, stringas the DataSource for a DropDownList directly, i.e. without having to iterate through the the Dictionary's...
8
by: Almad | last post by:
Hello, I discovered this behaviour in dictionary which I find confusing. In SneakyLang, I've tried to extend dictionary so it visits another class after something is added: class...
18
by: Marko.Cain.23 | last post by:
Hi, I create a dictionary like this myDict = {} and I add entry like this: myDict = 1 but how can I empty the whole dictionary? Thank you.
3
by: mk | last post by:
Hello everyone, I'm storing functions in a dictionary (this is basically for cooking up my own fancy schmancy callback scheme, mainly for learning purpose): .... return "f2 " + arg .......
0
by: Calvin Spealman | last post by:
On Thu, Jul 17, 2008 at 7:45 AM, mk <mrkafk@gmail.comwrote: As was pointed out already, this is a basic misunderstanding of assignment, which is common with people learning Python. To your...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.