This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Allow str.endswith and str.startswith to accept an iterable
Type: enhancement Stage: resolved
Components: Interpreter Core Versions: Python 3.8
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: Nosy List: brett.cannon, matrixise, rhettinger, serhiy.storchaka, taleinat
Priority: low Keywords:

Created on 2018-08-01 20:47 by brett.cannon, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (7)
msg322887 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2018-08-01 20:47
str.endswith() and str.startswith() only takes str or a tuple of str. It might be nice to make this str or an iterable of str (and that order must be kept so that a string isn't treated as an iterable of length-1 str).
msg322906 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2018-08-02 02:54
ISTM that this would encourage silently inefficient coding patterns like:

   url.endswith({'.html', '.txt', '.php'})
   # The set object gets rebuilt on every call
   # and a new set iterator object gets built on every call.
   # Looping over the contents uses the slower iterator protocol
   # rather than the existing superfast PyTuple_GET_SIZE() and
   # PyTuple_GET_ITEM() macros which are entirely in-lined.

Do we have any known use cases or user requests where the existing API doesn't suffice?
msg322910 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-08-02 03:38
I have same concerns as Raymond.
msg322988 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2018-08-02 19:01
Teammate of mine tripped up against this because he tried to use a list.
msg323020 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2018-08-03 05:12
> Teammate of mine tripped up against this because he tried to use a list.

Then, I recommend we close this.  Accepting a list would have encouraged inefficient code (a tuple of constants can be peephole optimized but a list of constants is rebuilt on every call).  Also, the error message is very clear, so it is unlikely he was "tripped-up" for more than a few seconds.

    >>> 'hello'.startswith(['he', 'go'])
    Traceback (most recent call last):
      File "<pyshell#1>", line 1, in <module>
        'hello'.startswith(['he', 'go'])
    TypeError: startswith first arg must be str or a tuple of str, not list
msg323029 - (view) Author: Tal Einat (taleinat) * (Python committer) Date: 2018-08-03 06:05
I tend to agree.  ISTM that for users, understanding the error message and passing a tuple is trivial, while realizing the performance benefit of using a tuple rather than a list or set here is certainly non-trivial.
msg323032 - (view) Author: Stéphane Wirtel (matrixise) * (Python committer) Date: 2018-08-03 06:45
I also agree with the problem of performance, tuple and str are immutables and from that, we can optimize the generation of the bytecode.

Raymond, @Brett & @Serhiy, Can we close this issue?
History
Date User Action Args
2022-04-11 14:59:04adminsetgithub: 78493
2018-08-03 17:07:54brett.cannonsetstatus: open -> closed
resolution: wont fix
stage: resolved
2018-08-03 06:45:26matrixisesetnosy: + matrixise
messages: + msg323032
2018-08-03 06:05:02taleinatsetmessages: + msg323029
2018-08-03 05:12:47rhettingersetmessages: + msg323020
2018-08-02 19:01:33brett.cannonsetmessages: + msg322988
2018-08-02 06:49:34taleinatsetnosy: + taleinat
2018-08-02 03:38:09serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg322910
2018-08-02 02:54:24rhettingersetnosy: + rhettinger
messages: + msg322906
2018-08-01 20:47:42brett.cannoncreate