feature request: a better str.endswith

Bengt Richter bokr at oz.net
Mon Jul 21 21:38:35 EDT 2003


On 21 Jul 2003 10:18:57 -0700, mis6 at pitt.edu (Michele Simionato) wrote:
[...]
>chrisperkins37 at hotmail.com (Chris Perkins) wrote in message news:<45228044.0307210522.1ef95144 at posting.google.com>...
>> mis6 at pitt.edu (Michele Simionato) wrote in message news:<2259b0e2.0307200721.16ef2ea1 at posting.google.com>...
>> > Oops! My mistake, I forgot the islice; it should be
>> > 
>> > the=lambda pred,seq: list(itertools.islice(itertools.ifilter(pred,seq),0,1))
>> > 
>> > in such a way that we exit at the first hit, otherwise one could just use
>> > the standard "filter". 
>> 
>> How about:
>> 
>> def the(pred,seq): return True in itertools.imap(pred,seq)
>> 
>> if you really want to use the name "the" ("any" makes much more sense to me).
>> 
>> Chris
>
>
>That's a good idea, indeed. BTW, in this context I feel that
>
> if the(filename.endswith, ('.jpg','.jpeg','.gif','.png')):
>    dosomething()
>
>is more clear than 
>
> if any(filename.endswith, ('.jpg','.jpeg','.gif','.png')):
>    dosomething()
>
>which is confusing to me since it seems "any" is referred to "filename" 
>whereas it is referred to the tuple elements.
>
I think I'd prefer

  if any_true(filename.endswith, ('.jpg','.jpeg','.gif','.png')):
     dosomething()

I suspect it will more often make sense read aloud in the general

  if any_true(pred, seq):

than

  if the(pred, seq)

I guess the full set of functions might be
any_true, any_false, all_true, and all_false.

or maybe someone can think of better short phrase?

Regards,
Bengt Richter




More information about the Python-list mailing list