[Numpy-discussion] (a and b) != (b and a) ?

Scott Gilbert xscottg at yahoo.com
Wed Jun 12 16:52:06 CDT 2002


--- Chris Barker <Chris.Barker at noaa.gov> wrote:
> 
> Well, yes, but it wasn't possible with <,>,== and friends untill rich
> comparisons were added in Python 2.1. So I am still wondering why the
> same extension wasn't made to "and" and "or". In fact, given that Guido
> is adding a bool type, this may be a time to re-visit the question,
> unless there really is a compelling reason not to, which is quite
> likely. 
> 

The "and" and "or" operators do short circuit evaluation.  So in addition
to acting like boolean operations, they are also control flow.  For "and",
the second expression is not evaluated if the first one is false.  For
"or", the second expression is not evaluated if the first one is true.  

I'm not clever enough to figure out how an overloaded and/or operator could
implement control flow for the outer expressions.  The outer expressions
"self" and "other" would already be evaluated by the time your
__operator__(self, other) function was called.

C++ has overloadable && and || operators, but overloading them is frowned
on by many.  C++ has the advantage over Python in that it knows the actual
types at compile time.




__________________________________________________
Do You Yahoo!?
Yahoo! - Official partner of 2002 FIFA World Cup
http://fifaworldcup.yahoo.com




More information about the Numpy-discussion mailing list