xor operator

Dom Grigonis dom.grigonis at gmail.com
Mon Nov 13 20:00:26 EST 2023


I agree, from perspective of standard `all` and `any` use cases this does not seem very useful.

However, in my experience it has its usages. E.g.:
* If sum(map(bool, iterable) [> | <] n can be useful. Counting dead processes and similar, optimisation problems where need to re-initialise if less than certain number of nodes reached certain threshold.
* If sum(map(bool, iterable) [== | !=] n is an edge case. Can’t even think of an example
* Finally, it would be a convenient shorthand for `bool(a) ^ bool(b)`

I sometimes think that there is also a case when certain function is not in one’s mind he doesn’t see the usefulness, but sometimes the sheer knowledge of its existence can twist certain situations in a positive manner.

What about an alternative, which does seem more useful:

def count_compare(iterable, n):
    return np.sign(sum(map(bool, iterable)) - n)

print(count_compare([False, False, False], 1))
As I am here, I will dare to ask if there is no way that `sign` function is going to be added to `math` or `builtins`. `np.sign` does the trick, but it is very slow to be used on a single number. And yes, I know that `copysign` exists, it just doesn’t do the same thing.

DG

> On 14 Nov 2023, at 02:33, Mats Wichmann via Python-list <python-list at python.org> wrote:
> 
> On 11/13/23 16:24, Dom Grigonis via Python-list wrote:
>> I am not arguing that it is a generalised xor.
>> I don’t want anything, I am just gauging if it is specialised or if there is a need for it. So just thought could suggest it as I have encountered such need several times already.
>> It is fairly clear by now that it is not a common one given it took some time to even convey what I mean. Bad naming didn’t help ofc, but if it was something that is needed I think it would have clicked much faster.
> 
> There are things that If You Need Them You Know, and If You Do Not You Do Not Understand - and you seem to have found one.  The problem is that forums like this are not a statistically great sampling mechanism - a few dozen people, perhaps, chime in on many topics; there are millions of people using Python. Still, the folks here like to think they're at least somewhat representative :)
> 
> Hardware and software people may have somewhat different views of xor, so *maybe* the topic title added a bit to the muddle.  To me (one of those millions), any/all falsy, any/all truthy have some interest, and Python does provide those. Once you get into How Many True question - whether that's the odd-is-true, even-is-false model, or the bail-after-X-truthy-values model, it's not terribly interesting to me: once it gets more complex than an all/any decision, I need to check for particular combinations specifically. Two-of-six means nothing to me until I know which combination of two it is.
> 
> 
> -- 
> https://mail.python.org/mailman/listinfo/python-list



More information about the Python-list mailing list