[Python-ideas] Should bool continue to inherit from int?
Steven D'Aprano
steve at pearwood.info
Wed Jan 21 12:47:28 CET 2015
On Wed, Jan 21, 2015 at 03:32:49AM -0800, Michael Mitchell wrote:
> PEP 285 <http://legacy.python.org/dev/peps/pep-0285> provides some
> justification for why arithmetic operations should be valid on bools and
> why bool should inherit from int (see points (4) and (6) respectively).
> Since it's been 12 years (it's possible this has been brought up again
> between now and then), I thought it could be worthwhile to take another
> look.
>
> I am mostly interested in a resurveying of the questions:
> 1) Would it still be very inconvenient to implement bools separately from
> ints?
Yes, it would break a lot of code that for the last twelve years has
relied on being able to perform arithmetic on bools. Arguably, Python 3
might have been the opportunity to break that backwards compatibility,
but we're up to 3.4 now so we would need a very good reason to do so.
> 2) Do most Python users still agree that arithmetic operations should be
> supported on booleans?
I don't know about "most", but this Python user thinks so.
> Follow-up thought:
> Something I noticed is that with PEP 484
> <https://www.python.org/dev/peps/pep-0484/> (Type Hints) specified as is,
> there would be no way to statically verify that a function will only
> operate on ints and not bools.
If you want an int, I'm not sure why you would want to exclude bools
since they are ints.
> An example would be a function that can only
> operate on integer values in a JSON dict created by the builtin `json`
> module (using the default decoder) cannot exist, as that function could
> operate on the boolean values of the dict.
I'm afraid I don't quite follow this example. Can you give some sample
code (with or without annotations)?
--
Steve
More information about the Python-ideas
mailing list