[ZODB-Dev] Persistent unhashable?

Greg Ward gward@mems-exchange.org
Tue, 3 Jul 2001 20:03:46 -0400


On 03 July 2001, Christian Robottom Reis said:
> I'm trying to create a simple hash of persistent objects to other
> persistent object. Something like:
[...]
> raises an AttributeError: __hash__ error! Why?
> 
> Is this a known limitation of Persistent?

It's been known to me for a while.  I finally got around to "fixing" it
for our project by adding this method:

    def __hash__ (self):
        if self._p_oid:
            return hash(self._p_oid)
        else:
            return id(self)

to our standard persistent base class.  (Rather than having all of our
database classes derive directly from Persistent, we use an intermediary
class, precisely to allow this sort of customization.  Phew, that was a
lucky design choice!)

Is there any reason Persistent doesn't do this for us automatically?  Or
was it just an oversight?

        Greg
-- 
Greg Ward - software developer                gward@mems-exchange.org
MEMS Exchange                            http://www.mems-exchange.org