[docs] [issue17854] symmetric difference operation applicable to more than two sets

Terry J. Reedy report at bugs.python.org
Fri May 3 22:44:10 CEST 2013

Terry J. Reedy added the comment:

Sets have methods that do not have operators (such as len, isdisjoint),
operators that do not have non-special methods (such as in, <), and method-operator pairs that do the same thing (such as (union, |), (symmetric_difference, ^)). For the pairs, it gives the method signature and the *equivalent* operator expression. Since .union takes multiple 'other' args, the equivalent operator expression does too. Since .symmetric_difference only takes one 'other' arg, so does the expression.

A coherent proposal would change the method code and doc to the following:

symmetric_difference(other, ...)
set ^ other ^ ...
    Return a new set with elements in an odd number of the sets.

s={1,2, 5}
t={2,3, 5}
u={3,4, 5}
{1, 4, 5}

I believe the proposal was once considered, and rejected. An argument for is that the effect of chained symmetric differences is not obvious, as evidenced by Amit's mistaken characterization. I had to think a bit before I was sure of the answer. An argument against is that what one actually gets is seldom wanted, so that allowing more than two inputs to the method would have little benefit. 

What might be done is to document the symmetric different of multiple sets with a parenthetical comment such as

"(The symmetric difference of multiple sets, a ^ b ^ c ^ ..., is a new set with elements appearing in an odd number of input sets.)"

This would let people know what to expect from such expressions, in a situation where the effect is less obvious than usual.

nosy: +rhettinger, terry.reedy
stage:  -> patch review
versions: +Python 2.7, Python 3.4

Python tracker <report at bugs.python.org>

More information about the docs mailing list