[Python-ideas] Should bool continue to inherit from int?
Chris Barker
chris.barker at noaa.gov
Wed Jan 21 21:12:10 CET 2015
On Wed, Jan 21, 2015 at 11:11 AM, Guido van Rossum <guido at python.org> wrote:
> So I believe you are saying, suppose I have a function that takes an
> integer argument and then JSON-encodes it and sends it to some server that
> requires a JSON int. Now suppose you are adding type hints to your code,
> and you add ": int" to the parameter under discussion. And now suppose you
> have a faulty caller which calls this function with the argument set to
> True. This program will type-check correctly, because True is a bool which
> is a subclass of int, but it will run incorrectly, because (under this
> assumption) True will be converted to a JSON true value, which the server
> rejects.
>
This sounds like it may benefit from the issue under discussion for Cython
-- a way to specify an int only, and not allow subclasses.
Though I suppose in this case, you may want to allow other subclasses, just
not that one.
This hypothetical code could also to a cast/conversion to int anyway, which
is pretty much how you'd do it with duck typing and no type checking.
-Chris
--
Christopher Barker, Ph.D.
Oceanographer
Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception
Chris.Barker at noaa.gov
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150121/5fb976f8/attachment.html>
More information about the Python-ideas
mailing list