feature request: a better str.endswith
Irmen de Jong
irmen at -NOSPAM-REMOVETHIS-xs4all.nl
Fri Jul 18 09:39:17 EDT 2003
Jp Calderone wrote:
> On Fri, Jul 18, 2003 at 05:01:47AM -0700, Michele Simionato wrote:
>
>>I often feel the need to extend the string method ".endswith" to tuple
>>arguments, in such a way to automatically check for multiple endings.
>>For instance, here is a typical use case:
>>
>>if filename.endswith(('.jpg','.jpeg','.gif','.png')):
>> print "This is a valid image file"
>>
>>Currently this is not valid Python and I must use the ugly
>>
>>if filename.endswith('.jpg') or filename.endswith('.jpeg') \
>> or filename.endswith('.gif') or filename.endswith('.png'):
>> print "This is a valid image file"
>
>
> extensions = ('.jpg', '.jpeg', '.gif', '.png')
> if filter(filename.endswith, extensions):
> print "This is a valid image file
>
> Jp
>
Using filter Michele's original statement becomes:
if filter(filename.endswith, ('.jpg','.jpeg','.gif','.png')):
print "This is a valid image file"
IMHO this is simple enough to not require a change to the
.endswith method...
--Irmen
More information about the Python-list
mailing list