Arindam Roychowdhury Arindam Roychowdhury - 1 year ago 96
Python Question

Why is True + 2 , 3 or False + 2, 2

>>> True + 2
>>> False + 2

I can understand that somehow, True means 1 and False means 0 . So
does it mean, a Boolean and integer operation always gives an integer?

Answer Source

In python bool is a subclass of int, and therefor satisfies the "is-a" relation, meaning a bool is-a int.

To demonstrate:

issubclass(bool, int)
=> True
isinstance(True, int)
=> True

In practice this means that in any operation which works on an int, the int can be substituted with a bool.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download