Python operations fallback to the reverse operation when they are not
defined, so e.g., x + y does x.__add__(y) and if that returns
NotImplemented, it does y.__radd__(x).  For the comparison operators,
it does the flipped operator, so this would be (0).__le__(x + 3),
which returns NotImplemented, so it then calls (x + 3).__ge__(0),
which works. The only downside here is that you end up getting
inequality in the reversed order from what you wanted.

Aaron Meurer

On Sun, Mar 22, 2015 at 4:31 AM, Joachim Durchholz <j...@durchholz.org> wrote:
> Am 21.03.2015 um 20:05 schrieb Aaron Meurer:
>>
>> https://github.com/sympy/sympy/pull/9181
>
>
> I see, S(0) is not necessary after all.
>
> I stand corrected, but how does this work?
>
> It is doing
>
>> 0 <= x + 3
>
>
> which evaluates to
>
>> 0 <= Add(Symbol: x, Integer: 3)
>
> and then calls the __le__ of Add.
> However, in the Python docs, it says
>
>> a <= b is equivalent to a.__le__(b)
>
> so I'd have expected it to go into
>
> 0.__le__(Add(...))
>
> and try a numeric comparison.
>
> Does SymPy install its own __le__ on int? (Technically possible but I'd be
> shocked.)
> Is Python's int.__le__ checking the other operator's type and calling into
> it? (If yes, is there a reference to that behaviour, or is it a CPython
> implementation detail?)
>
> I'm confused...
>
> --
> You received this message because you are subscribed to the Google Groups
> "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to sympy+unsubscr...@googlegroups.com.
> To post to this group, send email to sympy@googlegroups.com.
> Visit this group at http://groups.google.com/group/sympy.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/sympy/550E8BE8.5050104%40durchholz.org.
>
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sympy+unsubscr...@googlegroups.com.
To post to this group, send email to sympy@googlegroups.com.
Visit this group at http://groups.google.com/group/sympy.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sympy/CAKgW%3D6LNZdoB%3DCqeDz%2BQ0V9%2BG9ztBmS7pQ-P5SY%2Bi_qz1Jwh1A%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to