Hi Travis,

On 2013-01-24, Travis Scrimshaw <[email protected]> wrote:
>> Isn't that a total mess? Is there a reason why UniqueRepresentation 
>> provides __eq__ and __hash__, but not __cmp__? 
>>
>
> I thought __cmp__() is only called if __eq__() is not implemented?

I did not check the documentation before writing the following, but I
think it is as follows:
 * If you do A==B then __eq__ is called, regardless of whether __cmp__
   is provided or not.
 * Your class will implicitly have __eq__, __le__, __ne__ and so on,
   if you implement __richcmp__
 * If you do not implement  __richcmp__ but you implement __cmp__ then
   again your class will implicitly have __eq__, __le__, ...
 * If you provide both __richcmp__ and __cmp__, then your __richcmp__
   will be used in A==B, A<=B etc., but your __cmp__ will be used in
   cmp(A,B).

And now the catch: In a Cython class, you can not directly implement
__eq__, but you *must* implement __richcmp__ instead. And that's a
problem when cythoning UniqueRepresentation, because currently it is a
Python class with __eq__, __ne__ and __hash__ implemented, but not with
__richcmp__. 

And the problem with UniqueRepresentation.__richcmp__ is that one has an
obvious default for __eq__ and __ne__ (namely comparison with "is" or
"is not"), but __le__ and other comparison must be provided in another
class, because there is no obvious default.

> I almost feel like we should try to push comparisons by "is" (identity)
> rather than  "==" (equality) for unique parents.

Well, that is what UniqueRepresentation.__eq__ does anyway.

> Also please forgive the naive question, but is there any reason for a 
> Parent to not be a UniqueRepresentation?

For a very simple reason: If your Parent is a cdef class then it simply
can not inherit from UniqueRepresentation, because it is a Python class.

This problem will not be lifted. Even though I will try to provide a
cythoned UniqueRepresentation.__richcmp__ method, it would be impossible
to turn the Python class UniqueRepresentation into a cdef class: It
relies on a metaclass, and that's not available for extension classes.

Best regards,
Simon


-- 
You received this message because you are subscribed to the Google Groups 
"sage-devel" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
Visit this group at http://groups.google.com/group/sage-devel?hl=en.


Reply via email to