M
mk said:True
Huh?!
Regards,
mk
True
Huh?
True
Huh?!
Arnaud said:True
So what's your question?
Despite there are good reasons for bool to be int, the newcomer 'wtf'Steven said:True
Huh?
Yes. Do you have an actual question?
True
Huh?!
Exactly.
Bools are a late-comer to Python. For historical and implementation
reasons, they are a subclass of int, because it was normal for people to
use 0 and 1 as boolean flags, and so making False == 0 and True == 1 was
the least likely to break code.
E.g. back in the day, you would have something like:
{2:None}.has_key(2) -> 1
So folks would do:
print "The key is", ["missing", "present"][d.has_key(key)]
Which still works even now that has_key returns True or False rather than
1 or 0.
Well nothing I'm just kind of bewildered: I'd expect smth like that in Perl,
but not in Python.. Although I can understand the rationale after skimming
PEP 285, I still don't like it very much.
FalseFalse
So, the pythonic way to check for True/False should be:
False
Why do you need to check for True/False?
Steven D'Aprano wrote:
Despite there are good reasons for bool to be int, the newcomer 'wtf'
reaction at first glance is legitimate.
Starting python from scratch, booleans would have not been a subclass of
int (just guessing though), 'cause it makes no sense from a design POV.
3>>> scores =[True, False, True, True, False]
>>> score = sum(scores)
>>> score
You should never check for "is" False/True but always check for
equality. The reason is that many types support the equality (__eq__)
and boolen (__bool__ in 3x) protocols. If you check equality these
will be invoked, if you check identity ("is") they won't.
You should never check for "is" False/True but always check for
equality. The reason is that many types support the equality (__eq__)
and boolen (__bool__ in 3x) protocols. If you check equality these will
be invoked, if you check identity ("is") they won't.
Never say never.
If you specifically want to test for True or False themselves, accepting
no substitutes, then using "is" is the obvious way, and using "==" is
clearly and obviously wrong because it does accept substitutes:
True
Steven D'Aprano wrote:Despite there are good reasons for bool to be int, the newcomer 'wtf'
reaction at first glance is legitimate.
Starting python from scratch, booleans would have not been a subclass of
int (just guessing though), 'cause it makes no sense from a design POV.
You are just guessing. I would argue for what we have. An example of its
usefulness:
3scores =[True, False, True, True, False]
score = sum(scores)
score
Bools are also usefully used as sequence indexes.
Yes, obviously if you _really_ mean to test if something has the
object identity of True or False then an "is" test is the way to go.
I'm just not sure why you would ever do that. Also, I'm not sure how
your assertion matches up with the examples; The examples test for
equality with a float that returns true for __eq__ and a Decimal that
returns false for __eq__.
Both "1.0" and "Decimal(0, 1)" will return
False if the test is "is True" or "is False."
So, the pythonic way to check for True/False should be:
False
instead of ==, right?
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.