[Python-ideas] Changing the meaning of bool.__invert__
Steven D'Aprano
steve at pearwood.info
Sat Apr 9 11:43:05 EDT 2016
On Fri, Apr 08, 2016 at 02:52:45PM +0900, Stephen J. Turnbull wrote:
> > After all, not withstanding their fancy string representation,
>
> I guess "fancy string representation" was the original motivation for
> the overrides. If the intent was really to make operator versions of
> logical operators (but only for true bools!), they would have fixed ~
> too.
No need to guess. There's a PEP:
https://www.python.org/dev/peps/pep-0285/
> > they behave like ints and actually are ints.
>
> I can't fellow-travel all the way to "actually are", though. bools
> are what we decide to make them.
I'm not talking about bools in other languages, or bools in Python in
some alternate universe. But in the Python we have right now, bools
*are* ints, no ifs, buts or maybes:
py> isinstance(True, int)
True
This isn't an accident of the implementation, it was an explicit
BDFL pronouncement in PEP 285:
6) Should bool inherit from int?
=> Yes.
Now I'll certainly admit that bools-are-ints is an accident of history.
Had Guido been more influenced by Pascal, say, and less by C, he might
have choosen to include a dedicated Boolean type right from the
beginning. But he wasn't, and so he didn't, and consequently bools are
now ints.
> I just don't see why the current
> behaviors of &|^ are particularly useful, since you'll have to guard
> all bitwise expressions against non-bool truthies and falsies.
flag ^ flag is useful since we don't have a boolean-xor operator and
bitwise-xor does the right thing for bools. And I suppose some people
might prefer & and | over boolean-and and boolean-or because they're
shorter and require less typing. I don't think that's a particularly
good reason for using them, and as you say, you do have to guard
against non-bools slipping, but Consenting Adults applies.
--
Steve
More information about the Python-ideas
mailing list