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: Support list as argument to .startswith()
Type: enhancement Stage: resolved
Components: Interpreter Core Versions: Python 3.6
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: Fred, jwilk, remi.lapeyre, rhettinger, xtreak
Priority: normal Keywords:

Created on 2019-02-05 08:36 by Fred, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (6)
msg334856 - (view) Author: Fred (Fred) Date: 2019-02-05 08:36
The "".startswith() method accepts a string or a tuple as a parameter.

Consider adding support for list as parameter.

Example:
"foo".startswith(["food", "for", "fast"])
msg334857 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2019-02-05 08:46
For subtle reasons, that would make us worse off.  Tuple of constants get precomputed but lists of constants have to be rebuilt.

Thank for the suggestion, but I think we should stick with the original design.
msg334858 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2019-02-05 08:47
Related discussion : issue34312
msg334859 - (view) Author: Fred (Fred) Date: 2019-02-05 09:38
A programmer want to instruct the computer to do something, without having to care about how it works.

Maybe the library could in the background convert the list to a tuple.

Like:
"foo".startswith(tuple(["food", "for", "fast"]))

But the programmer shouldn't have to worry about this. I have a list, I want to use the list. I shouldn't have to care about interpreter internals that have nothing to do with the intent of my code.

My code should clearly represent intent, not have boilerplate code or constructs to workaround interpreter internals.
msg334881 - (view) Author: Rémi Lapeyre (remi.lapeyre) * Date: 2019-02-05 18:20
> A programmer want to instruct the computer to do something, without having to care about how it works.

This is not true, as a programmer you need to choose carefully your data structures because they matter, for example there is a difference between doing a lookup on an indexed field in an SQL database and a field that is not indexed. As a programmer you care because it impacts the performance of your application.

The linked discussion explains the difference between list and tuple in this instance.

@rhettinger: while this is not hard to fix, there is no mention in the documentation about why this choice has been made. A beginner don't know about the internals of cPython, should we add a small note about the optimization done with tuple?
msg413510 - (view) Author: Fred (Fred) Date: 2022-02-18 20:51
> For subtle reasons, that would make us worse off.  Tuple of constants get precomputed but lists of constants have to be rebuilt.

So if a list is 20 times slower than a tuple, I can still do billions of computations in a second on a Raspberry Pi, so does it matter?

Being able to pass a list into .startswith() makes it more user-friendly and makes Python be more productive for the developer.
History
Date User Action Args
2022-04-11 14:59:10adminsetgithub: 80078
2022-02-18 20:51:43Fredsetmessages: + msg413510
2022-02-18 19:10:42jwilksetnosy: + jwilk
2019-02-05 18:20:03remi.lapeyresetnosy: + remi.lapeyre
messages: + msg334881
2019-02-05 09:38:53Fredsetmessages: + msg334859
2019-02-05 08:47:33xtreaksetnosy: + xtreak
messages: + msg334858
2019-02-05 08:46:33rhettingersetstatus: open -> closed

nosy: + rhettinger
messages: + msg334857

resolution: rejected
stage: resolved
2019-02-05 08:36:18Fredcreate