[ python-Feature Requests-1491485 ] str.startswith/endswith could take a tuple?

SourceForge.net noreply at sourceforge.net
Mon Jun 5 18:33:29 CEST 2006


Feature Requests item #1491485, was opened at 2006-05-19 05:24
Message generated for change (Comment added) made by rhettinger
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=355470&aid=1491485&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Python Library
Group: None
Status: Open
Resolution: None
Priority: 5
Submitted By: Tom Lynn (tlynn)
Assigned to: Nobody/Anonymous (nobody)
Summary: str.startswith/endswith could take a tuple?

Initial Comment:
In the same way that exceptions can have a tuple of 
types specified and isinstance can take a tuple of 
types, str.startswith and endswith could take a tuple 
of possible prefixes/suffixes.

One use-case::
    if filename.endswith(('jpg', 'gif', 'png')):
         # ...
vs:
    if (filename.endswith('jpg') or filename.endswith
('gif') or
        filename.endswith('png')):
        #...

Obviously it's not a huge improvement in clarity, but 
it does seem to be an improvement to me.


----------------------------------------------------------------------

>Comment By: Raymond Hettinger (rhettinger)
Date: 2006-06-05 11:33

Message:
Logged In: YES 
user_id=80475

The previous comment was just information.  I am +1 on the
proposal because the use case is so common and the proposed
form is concise, clear, and natural.

----------------------------------------------------------------------

Comment By: Raymond Hettinger (rhettinger)
Date: 2006-05-19 15:58

Message:
Logged In: YES 
user_id=80475

FWIW, in Py2.5, this would be written:

   if any(filename.startswith(s) for s in suffixes):
      . . .

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=355470&aid=1491485&group_id=5470


More information about the Python-bugs-list mailing list