feature request: a better str.endswith

Jp Calderone exarkun at intarweb.us
Fri Jul 18 08:19:49 EDT 2003


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

-- 
"Pascal is Pascal is Pascal is dog meat."
                -- M. Devine and P. Larson, Computer Science 340





More information about the Python-list mailing list