Re: how to overload operator (a x b)?

2009-08-08 Thread Peter Otten
dmitrey wrote:

 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


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to overload operator (a x b)?

2009-08-08 Thread alex23
Diez B. Roggisch de...@nospam.web.de wrote:
 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...)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to overload operator (a x b)?

2009-08-07 Thread Diez B. Roggisch

dmitrey schrieb:

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


http://docs.python.org/reference/datamodel.html#object.__lt__

Diez
--
http://mail.python.org/mailman/listinfo/python-list


Re: how to overload operator (a x b)?

2009-08-07 Thread Benjamin Kaplan
On Fri, Aug 7, 2009 at 8:00 AM, dmitreydmitrey.kros...@scipy.org wrote:
 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.
 --
 http://mail.python.org/mailman/listinfo/python-list

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to overload operator (a x b)?

2009-08-07 Thread alex23
On Aug 7, 10:50 pm, Benjamin Kaplan benjamin.kap...@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.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to overload operator (a x b)?

2009-08-07 Thread Diez B. Roggisch

alex23 schrieb:

On Aug 7, 10:50 pm, Benjamin Kaplan benjamin.kap...@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.


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


Diez
--
http://mail.python.org/mailman/listinfo/python-list


Re: how to overload operator (a x b)?

2009-08-07 Thread Robert Lehmann
On Fri, 07 Aug 2009 08:50:52 -0400, Benjamin Kaplan wrote:

 On Fri, Aug 7, 2009 at 8:00 AM, dmitreydmitrey.kros...@scipy.org
 wrote:
 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.

Python actually allows you to chain comparison operators, automatically 
unpacking ``a  b  c`` to ``a  b and b  c``::

 class C(object):
...   def __lt__(self, other):
... print self, LESS-THAN, other
... return True
... 
 a = C(); b = C(); x = C()
 a  x  b
__main__.C object... LESS-THAN __main__.C object...
__main__.C object... LESS-THAN __main__.C object...
True

 x = 42
 40  x  50 # between 40 and 50
True
 50  x  60 # between 50 and 60
False
 1 == True  2 == 2.0  3  4 != 5  0 # yikes, unreadable! but legal.
True
 # same as: (1 == True) and (True  2) and (2 == 2.0) ...

HTH,

-- 
Robert Stargaming Lehmann

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to overload operator (a x b)?

2009-08-07 Thread Scott David Daniels

Benjamin Kaplan wrote:

 Python does not support compound
comparisons like that. You have to do a  b and b  c.


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.

 'a' 'd' 'z'
True
 'a' 'D' 'z'
False

--Scott David Daniels
scott.dani...@acm.org
--
http://mail.python.org/mailman/listinfo/python-list


Re: how to overload operator (a x b)?

2009-08-07 Thread exarkun

On 12:50 pm, benjamin.kap...@case.edu wrote:
On Fri, Aug 7, 2009 at 8:00 AM, dmitreydmitrey.kros...@scipy.org 
wrote:

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.


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:

  1  2  3
 True
  1  3  2
 False
  1 == 2 == 3
 False
  2 == 2 == 2
 True
 
Jean-Paul
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to overload operator (a x b)?

2009-08-07 Thread Steven D'Aprano
On Fri, 07 Aug 2009 08:04:22 -0700, Scott David Daniels wrote:

 Benjamin Kaplan wrote:
  Python does not support compound comparisons like that. You have
 to do a  b and b  c.
 
 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, Amsterdam

 2  3  4
1


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


-- 
Steven
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to overload operator (a x b)?

2009-08-07 Thread Carl Banks
On Aug 7, 7:18 am, Diez B. Roggisch de...@nospam.web.de wrote:
 alex23 schrieb:





  On Aug 7, 10:50 pm, Benjamin Kaplan benjamin.kap...@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.

 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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to overload operator (a x b)?

2009-08-07 Thread Carl Banks
On Aug 7, 9:01 pm, Carl Banks pavlovevide...@gmail.com wrote:
 On Aug 7, 7:18 am, Diez B. Roggisch de...@nospam.web.de wrote:





  alex23 schrieb:

   On Aug 7, 10:50 pm, Benjamin Kaplan benjamin.kap...@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.

  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.

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
-- 
http://mail.python.org/mailman/listinfo/python-list