Since the introduction of bool
, it has been a subclass of int
, and bools can be “cast” into integers implicitly:
>>> issubclass(bool, int)
True
>>> ['one', 'two'][False]
'one'
>>> ['one', 'two'][True]
'two'
>>> True/20
0.05
This was for historical reasons: compatibility with pre-2.3 APIs; and I understand it was kept from 2.3 to 2.7. (This was addressed in this question from 2011)
However, why is it still true in Python 3? I do not see any advantage to it. And there is no reason to keep that for backward compatibility: Python 3.0 was a breaking release; and I don't think any pre-2.3 API is still around anyway.