Logical AND on arrays
If ar1 and ar2 are arrays, are the following two expressions supposed to give the same result? (ar1 and ar2) logical_and(ar1,ar2) The first form seems to return the value of the second array, which isn't very useful. It would be nice to map the first expression to do what the first does. Tom Transue
Tom Transue
If ar1 and ar2 are arrays, are the following two expressions supposed to give the same result?
(ar1 and ar2)
logical_and(ar1,ar2)
The first form seems to return the value of the second array, which isn't very Only if the boolean value of the first array is true (i.e. iff not all its values are nonzero).
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. While each class is free to define a truth testing method (`__nonzero__`, or `__len__` if `__nonzero__` doesn't exist; the convention is that empty containers and zero numbers are false and pretty much everything else is true), 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). If this is still a bit unclear, I think this question has been raised before in this list, so you should be able to find a thread in the archive. cheers, alex -- Alexander Schmolck Postgraduate Research Student Department of Computer Science University of Exeter A.Schmolck@gmx.net http://www.dcs.ex.ac.uk/people/aschmolc/
Alexander Schmolck wrote:
Tom Transue
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@noaa.gov
participants (3)
-
Alexander Schmolck
-
Chris Barker
-
Tom Transue