[Python-ideas] 1 + True = 2

Giampaolo Rodola' g.rodola at gmail.com
Sun Jun 5 14:37:29 EDT 2016


On Sun, Jun 5, 2016 at 7:23 PM, Steven D'Aprano <steve at pearwood.info> wrote:

> 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?
>

I've read through the PEP and I understand the rationale about why
True/False is a subclass of int and I'm OK with that. PEP-285 at chapter 6
suggests a strong practical reason for this:

*       In an ideal world, bool might be better implemented as a*
*       separate integer type that knows how to perform mixed-mode*
*       arithmetic.  However, inheriting bool from int eases the*
*       implementation enormously (in part since all C code that calls*
*       PyInt_Check() will continue to work -- this returns true for*
*       subclasses of int)*

What I find odd though is that a bool can be used in arithmetical
operations as if it was the exact same thing as a number. I mean, to me
this is just odd:

>>> 1 + True
2

I would have expected that to be treated the same as:

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

A bool may be a subclass of int but conceptually (to me at least) it's
different almost the same way "1" is different than 1. I say "almost" only
because bool is a subclass of int, but then again, to my understanding that
was done for practical reasons (implementation details), not because the
main intention was to explicitly allow mixing bools and numbers in
arithmetical operations.

On the other hand (and I'm gonna contradict myself with what I've just said
above) on chapter 4:

*       There's a small but vocal minority that would prefer to see*
*       "textbook" bools that don't support arithmetic operations at*
*       all, but most reviewers agree with me that bools should always*
*       allow arithmetic operations.*

...so maybe supporting arithmetical operations was also a primary
intention, in which case my question is "why?".

-- 
Giampaolo - http://grodola.blogspot.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160605/165a08c5/attachment.html>


More information about the Python-ideas mailing list