[ZODB-Dev] Re: getting the object ID (_p_oid attribute)

Tres Seaver tseaver at palladion.com
Mon Jun 26 15:23:00 EDT 2006


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Robert Gravina wrote:
> 
> On 2006/06/27, at 3:49, Benji York wrote:
> 
>> Robert Gravina wrote:
>>> I just tried loading a persisted  object interactively and noticed
>>> that although the _p_oid doesn't  print out as anything (and hence I
>>> always thought it was empty in my  debugging prints), it isn't
>>> actually None! Can anyone explain this?  (here "p" is my persisted
>>> object)
>>>  >>> p._p_oid
>>> '\x00\x00\x00\x00\x00\x00\x00\x08'
>>>  >>> print p._p_oid
>>>  >>> p._p_oid is None
>>> False
>>
>> What would you expect to see if you printed out seven null characters
>> and a backspace?
> 
> Hahaha - that's a good point! I was expecting IDs to look, well,
> something like "asdf23asdf". Well, anyway thankyou! I seemed to have
> solved this problem. I was able to write a __eq__ function like this:
> 
>     def __eq__(self, other):
>         if isinstance(other, <name of my class>):
>             if hasattr(other,"_p_oid") and other._p_oid != None and
> (other._p_oid == self._p_oid):
>                 return True
>             else:
>                 return False
> 
> and now can compare objects for equality after the (Twisted) client
> edits them and sends them back.
> 
> Anyway, thanks again.

Note that 'hasattr' swallows *all* exceptions, including some which
might not be appropriate.  I think I would recommend:

  if isinstance(other, <my class>):
     o_jar = getattr(other, '_p_jar', None)
     o_oid = getattr(other, '_p_oid', None)
     return (
       o_jar and o_oid
        and o_jar == self._p_jar and o_oid == self._p_oid)

since OIDs are guaranteed to be unique only within the scope of a given
database (the '_p_jar' is a connection to the database).



Tres.
- --
===================================================================
Tres Seaver          +1 202-558-7113          tseaver at palladion.com
Palladion Software   "Excellence by Design"    http://palladion.com
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFEoDQU+gerLs4ltQ4RAnrPAKCuR/ZL+btXAzz6tQ/TPhycLbGM5gCglPXB
f/O+EhSS2QgfEyikJQi7wqA=
=meKl
-----END PGP SIGNATURE-----



More information about the ZODB-Dev mailing list