Proposal: s1.intersects(s2)

David Abrahams dave at boost-consulting.com
Thu Jul 5 05:53:15 CEST 2007


on Wed Jul 04 2007, "Steven D'Aprano" <steve-AT-REMOVE.THIS.cybersource.com.au> wrote:

> On Wed, 04 Jul 2007 14:37:34 +0000, Marc 'BlackJack' Rintsch wrote:
>
>> On Wed, 04 Jul 2007 09:59:24 -0400, David Abrahams wrote:
>> 
>>> Here's an implementation of the functionality I propose, as a
>>> free-standing function:
>>> 
>>>         def intersects(s1,s2):
>>>             if len(s1) < len(s2):
>>>                 for x in s1:
>>>                     if x in s2: return True
>>>             else:
>>>                 for x in s2:
>>>                     if x in s1 return True
>>>             return False
>> 
>> In Python 2.5 this can be written a bit more concise:
>> 
>> def intersects(set_a, set_b):
>>     if len(set_a) < len(set_b):
>>         set_a, set_b = set_b, set_a
>>     return any(item in set_a for item in set_b)
>
>
> I'd rather see sets gain an method "isintersect()" 

And why is that a good name?  It's not even grammatical.

> with the functionality than the language to gain a built-in
> function.

How is a method on sets not a built-in function?  Anyway, such a
method is what I'm proposing.

> However, there's a very subtle flaw in the idea. While "the intersection"
> of two sets is well-defined, "these two sets intersect" is (surprisingly!)
> _not_ well-defined. 

Depends how you define it.  I define it as "has a non-empty
intersection," which is pretty obviously solid.  The semantics should
be identical to 

  len(s1&s2) > 0

Those are exactly the semantics I want, and if mathematical purists
are going to argue that "intersects" is the wrong name, I ask that
they come up with a better one.

-- 
Dave Abrahams
Boost Consulting
http://www.boost-consulting.com

The Astoria Seminar ==> http://www.astoriaseminar.com




More information about the Python-list mailing list