From 5c4d3d0e4c6b533dbfbab36ad8034010fe90cf69 Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Mon, 9 Jun 2008 13:07:27 +0000 Subject: Let set.intersection() and set.intersection_update() take multiple input arguments. --- Doc/library/stdtypes.rst | 14 ++++++++++---- Lib/test/test_set.py | 6 ++++++ Misc/NEWS | 3 ++- Objects/setobject.c | 39 ++++++++++++++++++++++++++++++++++++--- 4 files changed, 54 insertions(+), 8 deletions(-) diff --git a/Doc/library/stdtypes.rst b/Doc/library/stdtypes.rst index 1d8e053..758ee7b 100644 --- a/Doc/library/stdtypes.rst +++ b/Doc/library/stdtypes.rst @@ -1575,11 +1575,14 @@ The constructors for both classes work the same: .. versionchanged:: 2.6 Accepts multiple input iterables. - .. method:: intersection(other) - set & other + .. method:: intersection(other, ...) + set & other & ... Return a new set with elements common to both sets. + .. versionchanged:: 2.6 + Accepts multiple input iterables. + .. method:: difference(other) set - other @@ -1639,11 +1642,14 @@ The constructors for both classes work the same: .. versionchanged:: 2.6 Accepts multiple input iterables. - .. method:: intersection_update(other) - set &= other + .. method:: intersection_update(other, ...) + set &= other & ... Update the set, keeping only elements found in it and *other*. + .. versionchanged:: 2.6 + Accepts multiple input iterables. + .. method:: difference_update(other) set -= other diff --git a/Lib/test/test_set.py b/Lib/test/test_set.py index 37a085c..729cc3b 100644 --- a/Lib/test/test_set.py +++ b/Lib/test/test_set.py @@ -103,6 +103,7 @@ class TestJointOps(unittest.TestCase): self.assertEqual(self.thetype('abcba').intersection(C('efgfe')), set('')) self.assertEqual(self.thetype('abcba').intersection(C('ccb')), set('bc')) self.assertEqual(self.thetype('abcba').intersection(C('ef')), set('')) + self.assertEqual(self.thetype('abcba').intersection(C('cbcf'), C('bag')), set('b')) def test_isdisjoint(self): def f(s1, s2): @@ -429,6 +430,11 @@ class TestSet(TestJointOps): s = self.thetype('abcba') self.assertEqual(s.intersection_update(C(p)), None) self.assertEqual(s, set(q)) + ss = 'abcba' + s = self.thetype(ss) + t = 'cbc' + self.assertEqual(s.intersection_update(C(p), C(t)), None) + self.assertEqual(s, set('abcba')&set(p)&set(t)) def test_iand(self): self.s &= set(self.otherword) diff --git a/Misc/NEWS b/Misc/NEWS index 05f7419..8f4963b 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -12,7 +12,8 @@ What's New in Python 2.6 beta 1? Core and Builtins ----------------- -- The set methods, update() and union() now accept multiple arguments. +- Several set methods now accept multiple arguments: update(), union(), + intersection() and intersection_update(). - Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes. diff --git a/Objects/setobject.c b/Objects/setobject.c index 908a9a3..a892dc8 100644 --- a/Objects/setobject.c +++ b/Objects/setobject.c @@ -1306,6 +1306,26 @@ set_intersection(PySetObject *so, PyObject *other) return (PyObject *)result; } +static PyObject * +set_intersection_multi(PySetObject *so, PyObject *args) +{ + Py_ssize_t i; + PyObject *result = (PyObject *)so; + + Py_INCREF(so); + for (i=0 ; i