comparing booleans

G

Gerrit Holl

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.
 
E

Erik Max Francis

Gerrit said:
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 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
 
D

Dang Griffith

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))


Easily checked:

... 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

It's not always the same, as shown here:
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:False

--dang
 
D

Duncan Booth

Interestingly, and I'm not sure why:
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.
 
E

Erik Max Francis

Dang said:
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.
 
Z

zde

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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top