Don't bother defining certain special methods on ExtensionClasses, because they won't work. Most notably, the __cmp__ method on an ExtensionClass will never be called. Neither will the reversed versions of binary arithmetic operations, such as __radd__ and __rsub__.
This is a moderately annoying limitation. It means that the
PersistentList class can't implement comparisons with regular
sequence objects, and therefore statements such as
if perslist==[]
don't do what you expect; instead of performing the correct
comparison, they return some arbitrary fixed result, so the if
statement will always be true or always be false. There is no good
solution to this problem at the moment, so all you can do is design
class interfaces that don't need to overload
__cmp__ or the __r*__ methods.
This limitation is mostly Python's fault. As of Python 2.1, the most
recent version at this writing, the code which handles comparing two
Python objects contains a hard-wired check for objects that are class
instances, which means that type(obj) == types.InstanceType
.
The code inside the Python interpreter looks like this:
/* Code to compare objects v and w */ if (PyInstance_Check(v) || PyInstance_Check(w)) return PyInstance_DoBinOp(v, w, "__cmp__", "__rcmp__", do_cmp); /* Do usual Python comparison of v,w */ c = PyObject_Compare(v, w);
While ExtensionClasses try to behave as much like regular Python
instances as possible, they are still not instances, and
type() doesn't return the InstanceType
object, so
no attempt is ever made to call __cmp__. Perhaps Python 2.2
will repair this.