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

D

dmitrey

hi all,
is it possible to overload operator "< <"? (And other like this one,
eg "<= <=", "> >", ">= >=")
Any URL/example?
Thank you in advance, D.
 
P

Peter Otten

dmitrey said:
is it possible to overload operator "< <"? (And other like this one,
eg "<= <=", "> >", ">= >=")

No.

a < x < b

is a shortcut for

a < x and x < b

where x is of course evaluated only once.

Peter
 
B

Benjamin Kaplan

hi all,
is it possible to overload operator "<  <"? (And other like this one,
eg "<=  <=", ">  >", ">=  >=")
Any URL/example?
Thank you in advance, D.

That isn't an operator at all. Python does not support compound
comparisons like that. You have to do "a > b and b > c".
 
A

alex23

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:
True

This is a _very_ common pattern.
.... def __lt__(self, other):
.... print 'in lt'
.... return True
.... def __gt__(self, other):
.... print 'in gt'
.... return True
....in gt
in lt
Truein gt
in lt
True

dmitrey: Diez' advice was the best you received.
 
R

Robert Lehmann

That isn't an operator at all. Python does not support compound
comparisons like that. You have to do "a > b and b > c".

Python actually allows you to chain comparison operators, automatically
unpacking ``a > b > c`` to ``a > b and b > c``::
.... def __lt__(self, other):
.... print self, "LESS-THAN", other
.... return True
.... <__main__.C object...> LESS-THAN <__main__.C object...>

HTH,
 
D

Diez B. Roggisch

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

True

This is a _very_ common pattern.

... def __lt__(self, other):
... print 'in lt'
... return True
... def __gt__(self, other):
... print 'in gt'
... return True
...
in gt
in lt
True
in gt
in lt
True

dmitrey: Diez' advice was the best you received.

Not really. I didn't get the chaining, and Peter is right that for that
there is no real overloading.

Diez
 
A

alex23

Diez B. Roggisch said:
Not really. I didn't get the chaining, and Peter is right that for that
there is no real overloading.

I'm sorry, I don't really get why overloading lt & gt isn't an answer
to the OP's question... His terminology may not have been correct but
I'm not sure why it's not a sufficient response.

(then again it is a Friday night and I have been out drinking...)
 
E

exarkun

That isn't an operator at all. Python does not support compound
comparisons like that. You have to do "a > b and b > c".

That's partially correct. There is no "compound less than operator", or
whatever you want to call that. However, Python does support "compound
comparisons" like that:
Jean-Paul
 
S

Steven D'Aprano

Funny, my python does. This has been around a long time. I am not
certain whether 1.5.2 did it, but "chained comparisons" have been around
for a long time.

Yes it does:

$ python1.5
Python 1.5.2 (#1, Apr 1 2009, 22:55:54) [GCC 4.1.2 20070925 (Red Hat
4.1.2-27)] on linux2
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam1


Remembering that back then, 0 and 1 were used instead of False and True.
 
C

Carl Banks

alex23 schrieb:








Not really. I didn't get the chaining, and Peter is right that for that
there is no real overloading.

You can program __lt__, __gt__, and friends to return a closure with a
boolean value. See my upcoming reply to the author.


Carl Banks
 
C

Carl Banks

You can program __lt__, __gt__, and friends to return a closure with a
boolean value.  See my upcoming reply to the author.

Actually, scratch that. It won't work because the chained comparison
short-circuits. If you have __lt__ return a closure then a < b won't
work unless a < b is always true, which means it'll cause ordinary
binary comparison to fail. Oh well.


Carl Banks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top