> You could write it as a ^ (not b), as long as you don't mind it giving
back an integer rather than a bool.

Actually, that'll give back a bool if a is a bool (and (not b) produces a
bool); ^ is overridden for bool/bool operations and itself returns a bool.

On Tue, Feb 23, 2021 at 1:48 AM Chris Angelico <ros...@gmail.com> wrote:

> On Tue, Feb 23, 2021 at 12:14 PM Soni L. <fakedme...@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-le...@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/
>
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@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/QLYII65C56TRVBWPE3NRLC7IBEBECNPO/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to