Hi all,

I ran into a strange case.

Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit (Intel)] on win32
...
>>> -1 == True
False
>>> -1 == False
False

This works though:
>>> if -1:
    print "OK"

OK

After some head scratching, I realized that:
- bool is a subclass of int and that True and False evaluates to 1 and 0, so -1 is equal to neither; and
- The if -1: statement probably works by treating -1 as bool(-1).

But I can't help finding the former comparison behavior odd. I admit that it is odd to write such code but when someone writes -1 == True, the intention is clearly a boolean comparison, not a numerical value comparison, isn't it?

As far as I understand, to do this comparison, python is casting (loosely speaking) True to its numerical value, rather than casting -1 to its boolean value.

So, my question is: wouldn't it make more sense to do just the opposite, i.e. cast -1 to its boolean value and do a boolean comparison of the operands, when one of the operands is True or False?

Or is this too fancy? What do you think?


--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to