#15731: Too early coercion causes weird behavior of comparison
------------------------+----------------------------
Reporter: strenner | Owner:
Type: defect | Status: new
Priority: minor | Milestone: sage-6.1
Component: coercion | Keywords:
Merged in: | Authors:
Reviewers: | Report Upstream: N/A
Work issues: | Branch:
Commit: | Dependencies:
Stopgaps: |
------------------------+----------------------------
Let's say I want to create my own number objects and I want be able to
compare my objects with regular number types.
{{{#!python
class SpecialNumber():
def __init__(self, number):
self.number = number
def __lt__(self, other):
return self.number < other
def __gt__(self, other):
return self.number > other
}}}
When comparing an int and a SpecialNumber, and SpecialNumber is on the
left, everything works well, because SpecialNumber's comparison operators
are called. However, when the SpecialNumber is on the right, it fail,
i.e.:
{{{#!python
sage: a = SpecialNumber(3)
sage: a > 2
True
sage: 2 < a
False
}}}
The exact same code in pure python would return True, because there is no
{{{__lt__}}} method to call on an int that works, so instead {{{a > 2}}}
is tried which returns True.
Whereas Sage, after not being able to call the {{{__lt__}}} method of the
Integer object, tries coercion right away without trying to evaluate {{{a
> 2}}}. Since there is no coercion, it checks if the types of the two
objects are the same or something like that, and since not, it returns
false. I think the right behavior would be check first if {{{a > 2}}} can
be evaluated, and try to coerce only after that.
--
Ticket URL: <http://trac.sagemath.org/ticket/15731>
Sage <http://www.sagemath.org>
Sage: Creating a Viable Open Source Alternative to Magma, Maple, Mathematica,
and MATLAB
--
You received this message because you are subscribed to the Google Groups
"sage-trac" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sage-trac.
For more options, visit https://groups.google.com/groups/opt_out.