Is (-1 ==True) True or False? Neither
oktaysafak at superonline.com
Sun Jan 25 03:50:22 CET 2009
Terry Reedy wrote:
> Oktay Şafak wrote:
>> That's what I'm trying to say: it would be more meaningful if
>> int.__eq__ did a boolean comparison when the other operand is a boolean.
> For that to be done, int would have to know about its subclass, which
> generally is bad design.
Good point, but of course it can be done without making the int type
know about the specifics of its subclass.
>> The reason is that when someone writes (-1 == True) he is clearly,
>> definitely, absolutely asking for a boolean comparison, not a
>> numerical one.
> I would say that the person does not understand Python and that the code
> is probably buggy.
> > As I said before, this is weird code;
> It is a bad idea to accommodate the language and interpreter to weird
> code which should never be written.
Again, good point. Agreed.
>> Well, I agree that explicit is better than implicit: when one wants to
>> use the numerical values of True or False, he should make it explicit
>> and use int(True) or int(False).
> The reason to make bool a subclass of int is to avoid having to do that.
> You do not have to like this fact of Python but it has been decided and
> will not change.
> > We never write counter += True, for example.
> But you might write counter += name, where you know name is bound to a
> bool. As I said, issubclass(bool, int) just so one would not have to
> write counter += int(name).
OK, my fault. Arithmetical operations was not my point, I was just
trying to talk about integer values of True and False in an equality
comparison context with other numbers. Of course the fact that
issubclass(bool, int) is very handy in calculations like that.
>>> If you want to cast an object to a boolean, use bool() on it.
>> Well, I can tell you in response that if you are interested in a
>> boolean's *integer* value, than make it explicit and use int(True).
> A Python bool *is* an int already: isinstance(True, int) returns True!
I know, you don't need to shout. What I was trying to say is that using
the integer value of a boolean *in a comparison with a number* doesn't
make much sense *to me*. I am not trying to change Python or whatever, I
am extremely pleased with it as it is. But that doesn't mean I like each
and every decision that has gone into it. Like everybody else, there are
things that doesn't quite fit in for me and I am trying to understand
better. I just came across this behavior while trying something in the
interpreter and wanted to discuss with people more knowledgeable than
myself. Please keep calm!
More information about the Python-list