# and cleaned up the docstrings.
#
# - Raymond Hettinger added a number of speedups and other
-# bugs^H^H^H^Himprovements.
+# improvements.
__all__ = ['BaseSet', 'Set', 'ImmutableSet']
data[deepcopy(elt, memo)] = value
return result
- # Standard set operations: union, intersection, both differences
+ # Standard set operations: union, intersection, both differences.
+ # Each has an operator version (e.g. __or__, invoked with |) and a
+ # method version (e.g. union).
- def union(self, other):
+ def __or__(self, other):
"""Return the union of two sets as a new set.
(I.e. all elements that are in either set.)
"""
- self._binary_sanity_check(other)
+ if not isinstance(other, BaseSet):
+ return NotImplemented
result = self.__class__(self._data)
result._data.update(other._data)
return result
- __or__ = union
+ def union(self, other):
+ """Return the union of two sets as a new set.
- def intersection(self, other):
+ (I.e. all elements that are in either set.)
+ """
+ return self | other
+
+ def __and__(self, other):
"""Return the intersection of two sets as a new set.
(I.e. all elements that are in both sets.)
"""
- self._binary_sanity_check(other)
+ if not isinstance(other, BaseSet):
+ return NotImplemented
if len(self) <= len(other):
little, big = self, other
else:
data[elt] = value
return result
- __and__ = intersection
+ def intersection(self, other):
+ """Return the intersection of two sets as a new set.
- def symmetric_difference(self, other):
+ (I.e. all elements that are in both sets.)
+ """
+ return self & other
+
+ def __xor__(self, other):
"""Return the symmetric difference of two sets as a new set.
(I.e. all elements that are in exactly one of the sets.)
"""
- self._binary_sanity_check(other)
+ if not isinstance(other, BaseSet):
+ return NotImplemented
result = self.__class__([])
data = result._data
value = True
data[elt] = value
return result
- __xor__ = symmetric_difference
+ def symmetric_difference(self, other):
+ """Return the symmetric difference of two sets as a new set.
- def difference(self, other):
+ (I.e. all elements that are in exactly one of the sets.)
+ """
+ return self ^ other
+
+ def __sub__(self, other):
"""Return the difference of two sets as a new Set.
(I.e. all elements that are in this set and not in the other.)
"""
- self._binary_sanity_check(other)
+ if not isinstance(other, BaseSet):
+ return NotImplemented
result = self.__class__([])
data = result._data
value = True
data[elt] = value
return result
- __sub__ = difference
+ def difference(self, other):
+ """Return the difference of two sets as a new Set.
+
+ (I.e. all elements that are in this set and not in the other.)
+ """
+ return self - other
# Membership test