how to overload operator "< <" (a < x < b)?

alex23 wuwei23 at gmail.com
Fri Aug 7 09:48:20 EDT 2009


On Aug 7, 10:50 pm, Benjamin Kaplan <benjamin.kap... at case.edu> wrote:
> That isn't an operator at all. Python does not support compound
> comparisons like that. You have to do "a > b and b > c".

You know, it costs nothing to open up a python interpreter and check
your certainty:

>>> x = 10
>>> 1 < x < 20
True

This is a _very_ common pattern.

>>> class X(object):
...   def __lt__(self, other):
...     print 'in lt'
...     return True
...   def __gt__(self, other):
...     print 'in gt'
...     return True
...
>>> x = X()
>>> 1 < x < 20
in gt
in lt
True
>>> 20 < x < 1
in gt
in lt
True

dmitrey: Diez' advice was the best you received.



More information about the Python-list mailing list