xor operator

Chris Angelico rosuav at gmail.com
Mon Nov 13 18:12:56 EST 2023


On Tue, 14 Nov 2023 at 10:00, Dom Grigonis via Python-list
<python-list at python.org> wrote:
>
> I am not asking. Just inquiring if the function that I described could be useful for more people.
>
> Which is: a function with API that of `all` and `any` and returns `True` if specified number of elements is True.
>
> It is not a generalised `xor` in strict programatic space. I.e. NOT bitwise xor applied to many bits.
> This is more in line with cases that `any` and `all` builtins are used.
>

A generalization of XOR is exactly what Grant and I said, though: a
parity check. See for example:

https://en.wikipedia.org/wiki/Exclusive_or
https://reference.wolfram.com/language/ref/Xor.html

It tells you whether you have an odd or even number of true values.

Now, if you want something that short-circuits a counting function,
that's definitely doable, but it's a sum-and-compare, not xor. Also,
it's quite specialized so it's unlikely to end up in the stdlib.

ChrisA


More information about the Python-list mailing list