[Numpy-discussion] Logical AND on arrays

Chris Barker Chris.Barker at noaa.gov
Mon Sep 9 16:56:05 EDT 2002


Alexander Schmolck wrote:
> Tom Transue <transue at niehs.nih.gov> writes:
> > useful.  It would be nice to map the first expression to do what the first does.
> 
> Impossible. Python's `and` and `or` are shortcircuting operators with a fixed
> meaning. ``A and B`` only evaluates both ``A`` and ``B`` iff both have the
> boolean value 'True' (e.g. ``0 and 0/1`` is OK because the second part never
> gets evaluated), otherwise the first 'False` value is returned.

true. Another option, however is to use the bitwise and aperator, which
is overloaded:

A & B

This generally will work in the cases you are likely to use it, like:

if (A > 5) & (B < 5):

as you'll be &-ing arrays of zeros and ones.

> there is (luckily) no way to change the meaning of `and` and `or` (just
> as one (luckily) can't change what the `if` statement does).

personally, I don't think it's that lucky. I'd rather lose the slight
benifits of the sometimes confusing ability to short-circuit, and get
full operator overloading. I think "and" and "or" are more like "<" than
"if". However, if I had designed a language it would be a pretty
wretched mess, I'm sure.

-Chris

-- 
Christopher Barker, Ph.D.
Oceanographer
                                    		
NOAA/OR&R/HAZMAT         (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris.Barker at noaa.gov




More information about the NumPy-Discussion mailing list