[Python-ideas] 1 + True = 2

Steven D'Aprano steve at pearwood.info
Sun Jun 5 13:23:13 EDT 2016


On Sun, Jun 05, 2016 at 06:37:48PM +0200, Giampaolo Rodola' wrote:
> >>> 1 + True
> 2
> >>> 1 + False
> 1
> >>>
> 
> I bumped into this today by accident and I can't recall ever being aware of
> this. Why isn't this a TypeError in Python 3?

Because bool is a subclass of int, and has been since it was first 
introduced back in Python 2.2. I'm not quite sure what your question is 
about... are you asking what was the original justification for making 
True and False equal to 1 and 0? If so, see the PEP:

https://www.python.org/dev/peps/pep-0285/

Or do you mean to ask why it wasn't changed in Python 3? Well, changed 
to what? All the reasons given in the PEP still hold, and there's no 
really compelling reason to make bool something else. Since bools aren't 
broken, why change them?


-- 
Steve


More information about the Python-ideas mailing list