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

Chris Barker chris.barker at noaa.gov
Tue Feb 13 15:12:29 EST 2018


On Mon, Feb 12, 2018 at 10:07 PM, David Mertz <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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180213/601d1b7c/attachment.html>


More information about the Python-ideas mailing list