From 4267be6478d38445b8632e678e3cf459490bed6b Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Wed, 11 Jun 2008 10:30:54 +0000 Subject: Multi-arg form for set.difference() and set.difference_update(). --- Doc/library/stdtypes.rst | 18 ++++++++++------ Lib/test/test_set.py | 14 +++++++++++++ Misc/NEWS | 2 +- Objects/setobject.c | 53 +++++++++++++++++++++++++++++++++++------------- 4 files changed, 66 insertions(+), 21 deletions(-) diff --git a/Doc/library/stdtypes.rst b/Doc/library/stdtypes.rst index 758ee7b..64d3c32 100644 --- a/Doc/library/stdtypes.rst +++ b/Doc/library/stdtypes.rst @@ -1583,10 +1583,13 @@ The constructors for both classes work the same: .. versionchanged:: 2.6 Accepts multiple input iterables. - .. method:: difference(other) - set - other + .. method:: difference(other, ...) + set - other - ... - Return a new set with elements in the set that are not in *other*. + Return a new set with elements in the set that are not in the others. + + .. versionchanged:: 2.6 + Accepts multiple input iterables. .. method:: symmetric_difference(other) set ^ other @@ -1650,10 +1653,13 @@ The constructors for both classes work the same: .. versionchanged:: 2.6 Accepts multiple input iterables. - .. method:: difference_update(other) - set -= other + .. method:: difference_update(other, ...) + set -= other | ... - Update the set, removing elements found in *other*. + Update the set, removing elements found in others. + + .. versionchanged:: 2.6 + Accepts multiple input iterables. .. method:: symmetric_difference_update(other) set ^= other diff --git a/Lib/test/test_set.py b/Lib/test/test_set.py index b32d953..1b01954 100644 --- a/Lib/test/test_set.py +++ b/Lib/test/test_set.py @@ -149,6 +149,8 @@ class TestJointOps(unittest.TestCase): self.assertEqual(self.thetype('abcba').difference(C('efgfe')), set('abc')) self.assertEqual(self.thetype('abcba').difference(C('ccb')), set('a')) self.assertEqual(self.thetype('abcba').difference(C('ef')), set('abc')) + self.assertEqual(self.thetype('abcba').difference(), set('abc')) + self.assertEqual(self.thetype('abcba').difference(C('a'), C('b')), set('c')) def test_sub(self): i = self.s.difference(self.otherword) @@ -467,6 +469,18 @@ class TestSet(TestJointOps): self.assertEqual(s.difference_update(C(p)), None) self.assertEqual(s, set(q)) + s = self.thetype('abcdefghih') + s.difference_update() + self.assertEqual(s, self.thetype('abcdefghih')) + + s = self.thetype('abcdefghih') + s.difference_update(C('aba')) + self.assertEqual(s, self.thetype('cdefghih')) + + s = self.thetype('abcdefghih') + s.difference_update(C('cdc'), C('aba')) + self.assertEqual(s, self.thetype('efghih')) + def test_isub(self): self.s -= set(self.otherword) for c in (self.word + self.otherword): diff --git a/Misc/NEWS b/Misc/NEWS index f721122..afaf05b 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -13,7 +13,7 @@ Core and Builtins ----------------- - Several set methods now accept multiple arguments: update(), union(), - intersection() and intersection_update(). + intersection(), intersection_update(), difference(), and difference_update(). - Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes. diff --git a/Objects/setobject.c b/Objects/setobject.c index 1127680..f3eea21 100644 --- a/Objects/setobject.c +++ b/Objects/setobject.c @@ -1495,11 +1495,16 @@ set_difference_update_internal(PySetObject *so, PyObject *other) } static PyObject * -set_difference_update(PySetObject *so, PyObject *other) +set_difference_update(PySetObject *so, PyObject *args) { - if (set_difference_update_internal(so, other) != -1) - Py_RETURN_NONE; - return NULL; + Py_ssize_t i; + + for (i=0 ; i