446,187 Members | 1,038 Online
Need help? Post your question and get tips & solutions from a community of 446,187 IT Pros & Developers. It's quick & easy.

comparing booleans

 P: n/a Hi, is it proper to compare booleans? It is possible, of course, because they're compatible with numbers, but booleans aren't truly numbers. I'm tempted to write: return cmp(self.extends, other.extends) instead of if self.extends and not other.extends: return 1 else: return -1 # I've already verified self.extends != other.extends ....but somehow comparing Booleans doesn't feel right... Is my feeling correct? (Hmm, makes me wonder, for booleans, are != and ^ equal?) Gerrit. -- 174. If she bear no sons to her second husband, the sons of her first husband shall have the dowry. -- 1780 BC, Hammurabi, Code of Law -- PrePEP: Builtin path type http://people.nl.linux.org/~gerrit/c.../pep-xxxx.html Asperger's Syndrome - a personal approach: http://people.nl.linux.org/~gerrit/english/ Jul 18 '05 #1
5 Replies

 P: n/a Gerrit Holl wrote: is it proper to compare booleans? It is possible, of course, because they're compatible with numbers, but booleans aren't truly numbers. I'm tempted to write: return cmp(self.extends, other.extends) Even if you're seriously worried about the correctness of comparing Booleans, you can always explicitly turn them into integers: return cmp(int(self.extends), int(other.extends)) (Hmm, makes me wonder, for booleans, are != and ^ equal?) Easily checked: for x in (True, False): .... for y in (True, False): .... print x, y, x != y, x ^ y .... True True False False True False True True False True True True False False False False -- __ Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/ / \ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE \__/ Weakness of attitude becomes weakness of character. -- Albert Einstein Jul 18 '05 #2

 P: n/a On Wed, 28 Jan 2004 16:17:22 -0800, Erik Max Francis wrote: Gerrit Holl wrote: is it proper to compare booleans? It is possible, of course, because they're compatible with numbers, but booleans aren't truly numbers. I'm tempted to write: return cmp(self.extends, other.extends)Even if you're seriously worried about the correctness of comparingBooleans, you can always explicitly turn them into integers: return cmp(int(self.extends), int(other.extends)) (Hmm, makes me wonder, for booleans, are != and ^ equal?)Easily checked: for x in (True, False):... for y in (True, False):... print x, y, x != y, x ^ y...True True False FalseTrue False True TrueFalse True True TrueFalse False False False It's not always the same, as shown here: True != True != True False True ^ True ^ True True True != False != True != True False True ^ False ^ True ^ True True Not that I've never used this (cool) Python syntax in practice, but I thought it was worth mentioning that using != and ^ in boolean expressions does not always give the same result. It does give the same result when there are only two operands. Interestingly, and I'm not sure why: (True != True) != True True True != (True != True) True True != True != True False --dang Jul 18 '05 #3

 P: n/a Dang Griffith wrote in news:ea******************************@news.teranew s.com: Interestingly, and I'm not sure why: (True != True) != True True True != (True != True) True True != True != True False You can chain comparison operators in Python. e.g. a < b < c is the same as: (a < b) && (b < c) except that if b is an expression the first form evaluates it exactly once whereas the second form evaluates it either once or twice. You can use any comparison operators in this form, so your True!=True!=True is just shorthand for: (True!=True) && (True!=True) which is in turn equivalent to: False && (True!=True) and 'False && anything' gives False. Jul 18 '05 #4

 P: n/a Dang Griffith wrote: Not that I've never used this (cool) Python syntax in practice, but I thought it was worth mentioning that using != and ^ in boolean expressions does not always give the same result. It does give the same result when there are only two operands. This is because of operator chaining, which only exists a special case for the relational operators (==, !=, <, <=, >, >=). It's not actually a difference in the truth table; it's because chaining operators behave differently than other operators. -- __ Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/ / \ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE \__/ The work will teach you how to do it. -- (an Estonian proverb) Jul 18 '05 #5

 P: n/a Erik Max Francis wrote: This is because of operator chaining, which only exists a special case for the relational operators (==, !=, <, <=, >, >=). It's not actually a difference in the truth table; it's because chaining operators behave differently than other operators. Unfortunately, this mis-feature works for other operators as well: Since 'in' operator usually has higher precedence than '==', it's pretty annoying to see: Python 2.3.3 (#2, Jan 4 2004, 12:24:16) 'a' in 'abc' True 'a' in 'abc' == True False Jul 18 '05 #6