Proposal: s1.intersects(s2)

Nis Jørgensen nis at superlativ.dk
Thu Jul 5 04:49:04 EDT 2007


Steven D'Aprano skrev:
> On Wed, 04 Jul 2007 23:53:15 -0400, David Abrahams wrote:
> 
>> 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.
> 
> Neither is "It's not even grammatical", but all but purists use it
> regardless.
> 
> A common Python convention is to have test functions named something
> like "isfoo", e.g. str.isdigit(), isspace(), islower() etc. They're not
> exactly grammatical either, e.g. isdigit() should actually be "contains
> one or more digits, and nothing but digits". (Presumably the pedantically
> correct name was rejected as being too long.) I was just following that
> convention.

The problem is, these functions can be read as "X is [consisting only
of] digit[s]", "X is lower [case]" etc, where the bits in brackets have
been removed for brewity. In the case of "s1 is intersect s2" there is
no way I can see of adding words to get a correct sentence. The
"obvious" naming is "s1.intersects(s2)" which reads as "s1 intersects
s2", a perfectly cromulent sentence.

Nis



More information about the Python-list mailing list