Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1b9f5d4

Browse files
committed
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().
1 parent 81912d4 commit 1b9f5d4

2 files changed

Lines changed: 48 additions & 8 deletions

File tree

Lib/sets.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -363,25 +363,29 @@ def __hash__(self):
363363

364364
# In-place union, intersection, differences
365365

366-
def union_update(self, other):
366+
def __ior__(self, other):
367367
"""Update a set with the union of itself and another."""
368368
self._binary_sanity_check(other)
369369
self._data.update(other._data)
370370
return self
371371

372-
__ior__ = union_update
372+
def union_update(self, other):
373+
"""Update a set with the union of itself and another."""
374+
self |= other
373375

374-
def intersection_update(self, other):
376+
def __iand__(self, other):
375377
"""Update a set with the intersection of itself and another."""
376378
self._binary_sanity_check(other)
377379
for elt in self._data.keys():
378380
if elt not in other:
379381
del self._data[elt]
380382
return self
381383

382-
__iand__ = intersection_update
384+
def intersection_update(self, other):
385+
"""Update a set with the intersection of itself and another."""
386+
self &= other
383387

384-
def symmetric_difference_update(self, other):
388+
def __ixor__(self, other):
385389
"""Update a set with the symmetric difference of itself and another."""
386390
self._binary_sanity_check(other)
387391
data = self._data
@@ -393,9 +397,11 @@ def symmetric_difference_update(self, other):
393397
data[elt] = value
394398
return self
395399

396-
__ixor__ = symmetric_difference_update
400+
def symmetric_difference_update(self, other):
401+
"""Update a set with the symmetric difference of itself and another."""
402+
self ^= other
397403

398-
def difference_update(self, other):
404+
def __isub__(self, other):
399405
"""Remove all elements of another set from this set."""
400406
self._binary_sanity_check(other)
401407
data = self._data
@@ -404,7 +410,9 @@ def difference_update(self, other):
404410
del data[elt]
405411
return self
406412

407-
__isub__ = difference_update
413+
def difference_update(self, other):
414+
"""Remove all elements of another set from this set."""
415+
self -= other
408416

409417
# Python dict-like mass mutations: update, clear
410418

Lib/test/test_sets.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,10 @@ def test_union_non_overlap(self):
219219
self.set |= Set([8])
220220
assert self.set == Set([2, 4, 6, 8]), "Non-overlapping union"
221221

222+
def test_union_method_call(self):
223+
self.set.union_update(Set([3, 4, 5]))
224+
assert self.set == Set([2, 3, 4, 5, 6]), "Union method call"
225+
222226
def test_intersection_subset(self):
223227
self.set &= Set((2, 4))
224228
assert self.set == Set((2, 4)), "Subset intersection"
@@ -235,6 +239,10 @@ def test_intersection_non_overlap(self):
235239
self.set &= Set([8])
236240
assert self.set == empty_set, "Non-overlapping intersection"
237241

242+
def test_intersection_method_call(self):
243+
self.set.intersection_update(Set([3, 4, 5]))
244+
assert self.set == Set([4]), "Intersection method call"
245+
238246
def test_sym_difference_subset(self):
239247
self.set ^= Set((2, 4))
240248
assert self.set == Set([6]), "Subset symmetric difference"
@@ -251,6 +259,30 @@ def test_sym_difference_non_overlap(self):
251259
self.set ^= Set([8])
252260
assert self.set == Set([2, 4, 6, 8]), "Non-overlapping symmetric difference"
253261

262+
def test_sym_difference_method_call(self):
263+
self.set.symmetric_difference_update(Set([3, 4, 5]))
264+
assert self.set == Set([2, 3, 5, 6]), "Symmetric difference method call"
265+
266+
def test_difference_subset(self):
267+
self.set -= Set((2, 4))
268+
assert self.set == Set([6]), "Subset difference"
269+
270+
def test_difference_superset(self):
271+
self.set -= Set((2, 4, 6, 8))
272+
assert self.set == Set([]), "Superset difference"
273+
274+
def test_difference_overlap(self):
275+
self.set -= Set((3, 4, 5))
276+
assert self.set == Set([2, 6]), "Overlapping difference"
277+
278+
def test_difference_non_overlap(self):
279+
self.set -= Set([8])
280+
assert self.set == Set([2, 4, 6]), "Non-overlapping difference"
281+
282+
def test_difference_method_call(self):
283+
self.set.difference_update(Set([3, 4, 5]))
284+
assert self.set == Set([2, 6]), "Difference method call"
285+
254286
#==============================================================================
255287

256288
class TestMutate(unittest.TestCase):

0 commit comments

Comments
 (0)