3

I have always read that python has strict type checks -

>>> 1 + 'hello'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'str'

This is fine since we cannot add int to a string

But why is the following allowed ?

>>> True + False
1
>>> True + 0
1

Why is strict checking not supported while adding int to a boolean ?

4 Answers 4

8
>>> issubclass(bool, int)
True

That explains everything. That is, bool is a subclass of int. That's why you can use a bool anywhere an int is allowed.

For a lot of detail, see the PEP that introduced the type.

EDIT: gloss

Note that Python didn't have a bool type for the first decade of its life. Conceptually "true/false" operations usually returned 1 or 0 instead. And Python programmers exploited that as often as, say, K&R C programmers exploited it. For example,

sum(x < 2 for x in some_list)

returned the number of elements in some_list less than 2. When adding the bool type, of course operators like < had to be changed to return True or False instead, but an enormous amount of code relying on 1 or 0 return values would have been broken. That's a key reason for why bool was made a subtype of int, restricted to the values 0 and 1 (with the fancier names False and True).

Sign up to request clarification or add additional context in comments.

1 Comment

I see. Link explain everything ! Thanks @Tim. Getting to learn new things daily!
6

Because Python's boolean type is a subclass of integer.

Comments

1

True or False simply evaluates to:

>>> 1 or 0
1

and True + 0 evaluates to the same thing

eg:

>>> int(True)
1
>>> int(False)
0

The or statement returns True if one of the arguments is True

5 Comments

Here i think + stands as arithmetic operator only True + 0 evaluates to 1 and 0 - True evaluates to -1 so + is arithmetic not a Boolean operator
@amar then why does False or 55 evaluate to 55 and False + 55 also evaluate to 55? clarify a bit please...
True is Bool equivalent of 1 and false is bool equivalent of 0 to clarify pls do in Python True + True + True you will understand Python's boolean type is a subclass of integer.
You are absolutely correct when it comes to Boolean algebra but in python context its little different..
False or 55 evaluates to 55 for the same reason that False or 'hello' evaluates to 'hello'. It is a different operator from addition; you can do False + 55 only because bool is a subclass of int in Python. You cannot do False + 'hello'.
1

PEP 285:

This PEP proposes the introduction of a new built-in type, bool, with two constants, False and True. The bool type would be a straightforward subtype (in C) of the int type, and the values False and True would behave like 0 and 1 in most respects (for example, False==0 and True==1 would be true) except repr() and str(). All built-in operations that conceptually return a Boolean result will be changed to return False or True instead of 0 or 1; for example, comparisons, the "not" operator, and predicates like isinstance().

it's possible because True is also 1 and False is also 0 (and vice versa):

>>> 1 + False
1
>>>
>>> 1 + 1
2
>>> True + 2
3
>>> False
False
>>> False + 4
4
>>>
>>> type(True+1)
<type 'int'>
>>>

also you have to know that None type is not eqvivalent to 0:

>>> None + True
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'NoneType' and 'bool'
>>>
>>> None is 0
False

4 Comments

I agree, but I am asking why is it allowed ?
added pep 285 short summary
@TimPeters you are really Tim Peters?!?!
@ZagorulkinDmitry, I think so - but on the Web anyone can be anyone ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.