diff options
author | Raymond Hettinger <python@rcn.com> | 2002-08-24 06:19:02 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2002-08-24 06:19:02 (GMT) |
commit | 1b9f5d4c1a56a557fda85f796a40bd1c8dac6f70 (patch) | |
tree | 0b2caf5ec74ee3968604c007517a617bd3af4b9b | |
parent | 81912d4764eb8ccb1b069de46c7f78381f4b19a6 (diff) | |
download | cpython-1b9f5d4c1a56a557fda85f796a40bd1c8dac6f70.zip cpython-1b9f5d4c1a56a557fda85f796a40bd1c8dac6f70.tar.gz cpython-1b9f5d4c1a56a557fda85f796a40bd1c8dac6f70.tar.bz2 |
At Tim Peter's suggestion, propagated GvR's binary operator changes to
the inplace operators. The strategy is to have the operator overloading
code do the work and then to define equivalent method calls which rely on
the operators. The changes facilitate proper application of TypeError
and NonImplementedErrors.
Added corresponding tests to the test suite to make sure both the operator
and method call versions get exercised.
Add missing tests for difference_update().
-rw-r--r-- | Lib/sets.py | 24 | ||||
-rw-r--r-- | Lib/test/test_sets.py | 32 |
2 files changed, 48 insertions, 8 deletions
diff --git a/Lib/sets.py b/Lib/sets.py index 5ca85de..09d9918 100644 --- a/Lib/sets.py +++ b/Lib/sets.py @@ -363,15 +363,17 @@ class Set(BaseSet): # In-place union, intersection, differences - def union_update(self, other): + def __ior__(self, other): """Update a set with the union of itself and another.""" self._binary_sanity_check(other) self._data.update(other._data) return self - __ior__ = union_update + def union_update(self, other): + """Update a set with the union of itself and another.""" + self |= other - def intersection_update(self, other): + def __iand__(self, other): """Update a set with the intersection of itself and another.""" self._binary_sanity_check(other) for elt in self._data.keys(): @@ -379,9 +381,11 @@ class Set(BaseSet): del self._data[elt] return self - __iand__ = intersection_update + def intersection_update(self, other): + """Update a set with the intersection of itself and another.""" + self &= other - def symmetric_difference_update(self, other): + def __ixor__(self, other): """Update a set with the symmetric difference of itself and another.""" self._binary_sanity_check(other) data = self._data @@ -393,9 +397,11 @@ class Set(BaseSet): data[elt] = value return self - __ixor__ = symmetric_difference_update + def symmetric_difference_update(self, other): + """Update a set with the symmetric difference of itself and another.""" + self ^= other - def difference_update(self, other): + def __isub__(self, other): """Remove all elements of another set from this set.""" self._binary_sanity_check(other) data = self._data @@ -404,7 +410,9 @@ class Set(BaseSet): del data[elt] return self - __isub__ = difference_update + def difference_update(self, other): + """Remove all elements of another set from this set.""" + self -= other # Python dict-like mass mutations: update, clear diff --git a/Lib/test/test_sets.py b/Lib/test/test_sets.py index b56219b..9ff98a6 100644 --- a/Lib/test/test_sets.py +++ b/Lib/test/test_sets.py @@ -219,6 +219,10 @@ class TestUpdateOps(unittest.TestCase): self.set |= Set([8]) assert self.set == Set([2, 4, 6, 8]), "Non-overlapping union" + def test_union_method_call(self): + self.set.union_update(Set([3, 4, 5])) + assert self.set == Set([2, 3, 4, 5, 6]), "Union method call" + def test_intersection_subset(self): self.set &= Set((2, 4)) assert self.set == Set((2, 4)), "Subset intersection" @@ -235,6 +239,10 @@ class TestUpdateOps(unittest.TestCase): self.set &= Set([8]) assert self.set == empty_set, "Non-overlapping intersection" + def test_intersection_method_call(self): + self.set.intersection_update(Set([3, 4, 5])) + assert self.set == Set([4]), "Intersection method call" + def test_sym_difference_subset(self): self.set ^= Set((2, 4)) assert self.set == Set([6]), "Subset symmetric difference" @@ -251,6 +259,30 @@ class TestUpdateOps(unittest.TestCase): self.set ^= Set([8]) assert self.set == Set([2, 4, 6, 8]), "Non-overlapping symmetric difference" + def test_sym_difference_method_call(self): + self.set.symmetric_difference_update(Set([3, 4, 5])) + assert self.set == Set([2, 3, 5, 6]), "Symmetric difference method call" + + def test_difference_subset(self): + self.set -= Set((2, 4)) + assert self.set == Set([6]), "Subset difference" + + def test_difference_superset(self): + self.set -= Set((2, 4, 6, 8)) + assert self.set == Set([]), "Superset difference" + + def test_difference_overlap(self): + self.set -= Set((3, 4, 5)) + assert self.set == Set([2, 6]), "Overlapping difference" + + def test_difference_non_overlap(self): + self.set -= Set([8]) + assert self.set == Set([2, 4, 6]), "Non-overlapping difference" + + def test_difference_method_call(self): + self.set.difference_update(Set([3, 4, 5])) + assert self.set == Set([2, 6]), "Difference method call" + #============================================================================== class TestMutate(unittest.TestCase): |