xor operator

Grant Edwards grant.b.edwards at gmail.com
Mon Nov 13 19:18:18 EST 2023


On 2023-11-13, 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.

I've got no objection to a function that counts True objects returned
by an iterator and returns True IFF count == <N>.

I've got no objection to a function that counts True objects returned
by an iterator and returns True IFF count >= <N>.

I've got no objection if that latter function short-circuits by
stopping the iteration and returning True when it has seen <N> true
objects.

I don't recall ever having a need for such a function in the 25 years
I've been writing Python code, but I'm not going to claim that nobody
else has a need for such a function.

I would object to that being called 'xor', and would fight to the
death (yes, I'm being hyperbolic) the addition of a builtin with the
name 'xor' that does what you describe.

> It is not a generalised `xor` in strict programatic space.

AFAICT, it's not nothing at all to do with 'xor' in any sense.

> I.e. NOT bitwise xor applied to many bits.  This is more in line
> with cases that `any` and `all` builtins are used.

Except the 'any' and 'all' builtins are _exactly_ the same as bitwise
or and and applided to many bits. To do something "in line" with that
using the 'xor' operator would return True for an odd number of True
values and False for an even Number of True values.

--
Grant


More information about the Python-list mailing list