ExtensionClass/Persistent and __cmp__ is tricky

Jim Fulton jim at zope.com
Sun Jun 29 14:56:54 CEST 2003


Tim Peters wrote:
> [Christian Reis]
> 

....

>>It seems you can't compare ExtensionClass with basic types; in Python
>>2.1.3 at least it appears to me that try_3way_compare doesn't want to
>>call tp_compare on the second instance because it's not a PyInstance.
> 
> 
> Possibly so.  I don't think anyone really understands the details of
> ExtensionClass except for Jim (Fulton), and his understanding was fresh
> years ago -- ExtensionClass isn't part of Python, it's part of Zope.


ExtensionClass is a type.  There's this funny rule for types and
__cmp__ that __cmp__ is only called for types when:

- Comparing objects of the same type or

- One of the types is a number.

Otherwise, Python falls back to address comparison.

This rule is really annoying.  You can get around this, as I recall
by implementing some numeric methods (e.g. __nonzero__). That will make
Python call EC's __cmp__ more often.

__cmp__ should be deprecated. Unfortunately, EC doesn't support rich comparisons.
The good news is that soon, ZODB3 will support new-style classes.

Jim

-- 
Jim Fulton           mailto:jim at zope.com       Python Powered!
CTO                  (703) 361-1714            http://www.python.org
Zope Corporation     http://www.zope.com       http://www.zope.org






More information about the Python-list mailing list