[Python-ideas] Implement __add__ for set and frozenset

Brandon Mintern bmintern at gmail.com
Mon Jun 2 13:48:25 CEST 2008


I would like to propose that the + operator be a synonym for | (union)
on sets. IMHO, the succinct power of being able to say

sum(list_of_sets, set())

as opposed to

import operator
reduce(operator.and_, list_of_sets, set())

far outweighs any problems with having two operators for union. The
sum paradigm is certainly more readable as well. I realize that a
function named "unionall" could be defined trivially, but with a
built-in already serving the purpose in a readable and reasonable way,
it seems silly to not use it.

I anticipate that someone will ask why such a paradigm should be
available for union as opposed to the other set operations. My answer
is that several standard algorithms rely on a union over a sequence of
sets; one example is the construction of the "follow" set in
constructing an LL or SLR parser, and I know I've seen others that I
cannot remember off the top of my head. There is even a mathematical
symbol (big-U) for it akin to the summation sign (uppercase sigma).

Any feedback?

Brandon



More information about the Python-ideas mailing list