[Python-ideas] Boolean ABC similar to what's provided in the 'numbers' module

Sylvain MARIE sylvain.marie at schneider-electric.com
Wed Feb 14 03:38:01 EST 2018


My point is just that today, I use the ‘numbers’ package classes (Integral, Real, …) for PEP484 type-hinting, and I find it quite useful in term of input type validation (in combination with PEP484-compliant type checkers, whether static or dynamic). Adding a Boolean ABC with a similar behavior would certainly add consistency to that ‘numbers’ package – only for users who already find it useful, of course.

Note that my use case is not about converting an object to a Boolean, I’m just speaking about type validation of a ‘true’ boolean object, for example to be received as a function argument for a flag option. This is for example for users who want to define strongly-typed APIs for interaction with the ‘outside world’, and keep using duck-typing for internals.

Sylvain

De : Python-ideas [mailto:python-ideas-bounces+sylvain.marie=schneider-electric.com at python.org] De la part de Chris Barker
Envoyé : mardi 13 février 2018 21:12
À : David Mertz <mertz at gnosis.cx>
Cc : python-ideas <python-ideas at python.org>
Objet : Re: [Python-ideas] Boolean ABC similar to what's provided in the 'numbers' module



On Mon, Feb 12, 2018 at 10:07 PM, David Mertz <mertz at gnosis.cx<mailto:mertz at gnosis.cx>> wrote:
I'm not sure I'm convinced by Sylvain that Boolean needs to be an ABC in the standard library; Guido expresses skepticism.  Of course it is possible to define it in some other library that actually needs to use `isinstance(x, Boolean)` as Sylvain demonstraits in his post.  I'm not sure I'm unconvinced either, I can see a certain value to saying a given value is "fully round-trippable to bool" (as is np.bool_).

But is an ABC the way to do it? Personally, I'm skeptical that ABCs are a solution to, well, anything (as apposed to duck typing and EAFTP). Take Nick's example:

"""
The other comparison that comes to mind would be the distinction
between "__int__" ("can be coerced to an integer, but may lose
information in the process") and "__index__" ("can be losslessly
converted to and from a builtin integer").
"""

I suppose we could have had an Index ABC -- but that seems painful to me.

so maybe we could use a __true_bool__ special method?

(and an operator.true_bool() function ???)

(this all makes me wish that python bools were more pure -- but way to late for that!)

I guess it comes down to whether you want to:

 - Ask the question: "is this object a boolean?"

or

 - Make this object a boolean

__index__ (and operator.index())  is essentially the later -- you want to make an index out of whatever object you have, if you can do so.

-CHB



--

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<mailto:Chris.Barker at noaa.gov>

______________________________________________________________________
This email has been scanned by the Symantec Email Security.cloud service.
______________________________________________________________________
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180214/d793de92/attachment-0001.html>


More information about the Python-ideas mailing list