On Wed, 21 Sep 2005 18:53:34 +0000, Ron Adam wrote: > Steven D'Aprano wrote: > >>>So.. >>> >>> bool(a and b) * value >>> >>>Would return value or zero, which is usually what I want when I do this >>>type of expression. > >> That's all very interesting, and valuable advice for somebody who doesn't >> understand how Python's logical operators work, but the question is, when >> would you actually want that type of expression? > > It's a filter which returns a value or zero depending on conditions a and b.
Ah, that's a good example, thanks, except I notice you didn't actually cast to bool in them, eg: (min < value < max) * value You also said: > In boolean math it is useful to add and subtract. > >>> a = b = True > >>> a + b > 2 # Non boolean result. I presume you mean Boolean algebra by "Boolean math". I have to disagree with you there. It is *not* useful to do addition, subtraction, multiplication or division in Boolean algebra. Those operations aren't defined on Booleans, because they return results which aren't Booleans. If you wanted to extend arithmetic operations on Booleans, you would need to either redefine addition and multiplication as XOR and AND (as mathematicians do when they extend Boolean algebras to rings), or do your arithmetic modulo 2. I'm not saying that it can't be useful to treat Booleans as if they were the integers 0 and 1, but in mathematics Booleans are abstract values distinct from the integers (even when they use the symbols 0 and 1) and the concept "True plus True is two" is meaningless. It is useful to read the comments here: http://www.python.org/doc/2.3/whatsnew/section-bool.html eg "Python's Booleans were not added for the sake of strict type-checking. A very strict language such as Pascal would also prevent you performing arithmetic with Booleans, and would require that the expression in an if statement always evaluate to a Boolean result." > Should bool type act like bools as expressed here? > > http://www.ee.surrey.ac.uk/Projects/Labview/boolalgebra/ That is only one possible Boolean algebra, the simplest one. Strictly speaking, Booleans aren't limited to two values. See http://en.wikipedia.org/wiki/Boolean_algebra for more detail. Python's bools aren't Booleans. They are merely aliases for 0 and 1. -- Steven. -- http://mail.python.org/mailman/listinfo/python-list