[Python-ideas] Changing the meaning of bool.__invert__

Steven D'Aprano steve at pearwood.info
Thu Apr 7 21:53:44 EDT 2016


On Thu, Apr 07, 2016 at 01:17:57PM +0000, Antoine Pitrou wrote:
> Steven D'Aprano <steve at ...> writes:
> > 
> > > Numpy's boolean type does the more useful (and more expected) thing:
> > > 
> > > >>> ~np.bool_(True)
> > > False
> > 
> > Expected by whom?
> 
> By anyone who takes booleans at face value (that is, takes booleans as
> representing a truth value and expects operations on booleans to reflect
> the semantics of useful operations on truth values, not some arbitrary
> side-effect of the internal representation of a boolean...).

Bools in Python have *always* been integers, so who are these people 
taking booleans at face value? Beginners? If so, say so. That's a motive 
I can understand.

But I think that people who expect bools to be real truth values, like 
in Pascal, probably won't expect BITWISE operations to operate on them 
at all and will use the BOOLEAN operators and, or, not. Bitwise 
operators operate on a sequence of bits, not a single truth value. What 
do these naive "bools are truth values" people think:

True << 3 

should return?

I don't think we need a second way to spell "not bool". Bools have 
always been ints in Python, and apart from their fancy string 
representation they behave like ints. I don't think it helps to make ~ a 
special case where they don't.


> But I'm not surprised by such armchair commenting and pointless controversy
> on python-ideas, since that's what the list is for....

Thanks for your feedback, I'll give it the due consideration it 
deserves.


-- 
Steve


More information about the Python-ideas mailing list