Also, code that is expecting an int should not behave differently when it receives a bool.

On Mon, Feb 22, 2021 at 17:47 Chris Angelico <rosuav@gmail.com> wrote:
On Tue, Feb 23, 2021 at 12:14 PM Soni L. <fakedme+py@gmail.com> wrote:
>
> Currently ~False is -1 and ~True is -2. Would be nicer if ~bool was the
> same as not bool. Hopefully nobody actually relies on this but
> nevertheless, it would be a backwards-incompatible change so the whole
> deprecation warnings and whatnot would be required.

There are quite a few ways in which bitwise operators are not the same
as boolean operators. What would be the advantage of having them be
the same in just this one case?

> In particular, this is nice for xnor operator: a ^~ b. This currently
> works on ints, but not on bools, while most other operators, including
> xor, do successfully work on bools.

You could write it as a ^ (not b), as long as you don't mind it giving
back an integer rather than a bool. Fundamentally, you're doing
bitwise operations on integers, and expecting them to behave as if
they have only a single bit each, so another way to resolve this might
be to mask it off at the end with "& 1".

If this makes your code horrendously ugly, perhaps it would be better
to create your own "one-bit integer" class, which responds to all
bitwise operators by automatically masking down to one bit?

ChrisA
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-leave@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/6HLSLQMMKYNLSEQCIIDVKXIGV2TZFREE/
Code of Conduct: http://python.org/psf/codeofconduct/
--
--Guido (mobile)