[issue17854] symmetric difference operation applicable to more than two sets
New submission from Amit Saha:
The description of the symmetric difference operation implies that it cannot be applied to more than two sets (http://docs.python.org/3/library/stdtypes.html#set.symmetric_difference).
However, this is certainly possible:
s={1,2} t={2,3} u={3,4} s^t^u
{1, 4}
s.symmetric_difference(t).symmetric_difference(u)
{1, 4}
I am not sure how much of a "semantic" sense that makes, given that symmetric difference is by definition for two sets. (http://en.wikipedia.org/wiki/Symmetric_difference).
So, either the operator should be fixed to allow only two sets or the description be updated.
 assignee: docs@python components: Documentation messages: 187899 nosy: Amit.Saha, docs@python priority: normal severity: normal status: open title: symmetric difference operation applicable to more than two sets type: behavior versions: Python 3.3
_______________________________________ Python tracker report@bugs.python.org http://bugs.python.org/issue17854 _______________________________________
Amit Saha added the comment:
On some more thought, perhaps the description should be updated. Since s^t^u is effectively (s^t)^u and hence the implementation does not violate the definition.

_______________________________________ Python tracker report@bugs.python.org http://bugs.python.org/issue17854 _______________________________________
Georg Brandl added the comment:
Can you suggest a change? I don't see a problem here; giving multiple operators for the other operations does not imply that they are not treated as leftassociative.
 nosy: +georg.brandl
_______________________________________ Python tracker report@bugs.python.org http://bugs.python.org/issue17854 _______________________________________
Amit Saha added the comment:
I think the only change I am suggesting is the description of the ^ operator to be something like this:
set ^ other ^ .. Return a new set with elements from the sets which are not present in more than one set
I do understand that this is not really what the operator and the corresponding operation symmetric_difference() allows semantically. But it does make it consistent with the description of operators such as the  or &, but then their operation allows multiple sets semantically.
Hmm may be it is fine as it is..

_______________________________________ Python tracker report@bugs.python.org http://bugs.python.org/issue17854 _______________________________________
Terry J. Reedy added the comment:
Sets have methods that do not have operators (such as len, isdisjoint), operators that do not have nonspecial methods (such as in, <), and methodoperator 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} print(s^t^u)
{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@bugs.python.org http://bugs.python.org/issue17854 _______________________________________
Ezio Melotti added the comment:
Return a new set with elements in an odd number of the sets.
This wording is not really clear to me.
IMHO the documentation is fine as is. The evaluation order works as usual, and, since the symmetric difference is an associative (and commutative) operation, the order doesn't even matter.
 nosy: +ezio.melotti type: behavior > enhancement
_______________________________________ Python tracker report@bugs.python.org http://bugs.python.org/issue17854 _______________________________________
Terry J. Reedy added the comment:
If you take the union/intersection/symmetric difference of n sets, the result is a set with all items that appears in one/all/an odd number of the n sets. The union and intersection methods actually accept n inputs, because the result is obvious, useful, and can be obtained faster that with n1 binary operations. The symmetric_difference method does not, I presume because the result in not obvious (but that cuts both ways), not known to be useful, and perhaps would not be much faster than than n1 binary operations.

_______________________________________ Python tracker report@bugs.python.org http://bugs.python.org/issue17854 _______________________________________
Irit Katriel iritkatriel@yahoo.com added the comment:
I agree that the doc is fine as it is. If there will be no objections/suggestions in the next couple of weeks I will close this issue.
 nosy: +iritkatriel status: open > pending
_______________________________________ Python tracker report@bugs.python.org https://bugs.python.org/issue17854 _______________________________________
Change by Raymond Hettinger raymond.hettinger@gmail.com:
 resolution: > rejected stage: patch review > resolved status: pending > closed
_______________________________________ Python tracker report@bugs.python.org https://bugs.python.org/issue17854 _______________________________________
participants (6)

Amit Saha

Ezio Melotti

Georg Brandl

Irit Katriel

Raymond Hettinger

Terry J. Reedy