[Zope] objects as values in a python dictionary

Jim Washington jwashin@vt.edu
Wed, 20 Jun 2001 08:25:31 -0400


Peter Bengtsson wrote:

> This concerns both Python Script and External Methods. I haven't tried it
> with DTML (if possible).
> 
> A dictionary can use objects as values I think.
> res={}
> res[string] = "A wicked module I use often"
> res[context] = "This is where you were :)"
> That works, but it doesn't work when I loop over objectValues
> 
> for object in folderobject.objectValues():
>     res[object] = "yeah"
> 
> The error message is AttributeError and __hash__ something.
> What have I missed?

The keys of a python dict need to be immutable. The values can be anything.

objectValues() returns a list of objects, which are complex items, not 
the sort of thing allowable as a dictionary key.

The id of the object should work, since it is a string and thus immutable.

for object in folderobject.objectValues():
   res[object.getId()] = "yeah"

-- Jim Washington