-
-
Notifications
You must be signed in to change notification settings - Fork 28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Submission for SC consideration: PEP 616: String methods to remove prefixes and suffixes #23
Comments
Some context. Dennis Sweeney created https://bugs.python.org/issue39939 with an implementation. I asked for a PEP: https://bugs.python.org/issue39939#msg364313 The PEP got like 100 messages, so I consider that it was properly discussed :-) The method names changed from cutprefix/cutsuffix to removeprefix/removesuffix. I proposed to accept a tuple of str, like: removesuffix(("\n", "\r\n", "\r")). The PEP now has a good rationale to explain why the idea was abandoned: |
I looked in the stdlib which functions would benefit of removesuffix(). I only found 3 files in gzip:
pydoc:
tarfile:
But there are many functions which cannot use removesuffix(). Examples:
or:
or
|
I found more for the
|
Here are the matches for
|
I just realized I botched my ripgrep search, so there's more:
|
The full code is:
removesuffix() cannot be used directly here.
The full code is more complex:
removesuffix() cannot be used directly: it doesn't support regex. Same for the two following examples using mo.group()
Full code doesn't use endswith(), the suffix is always removed:
removesuffix() cannot be used.
removesuffix() cannot be used. I didn't check other examples. |
IMO these instances where it is not possible to use The |
I wrote https://github.com/python/cpython/pull/19455/files to see how the PEP 616 could be used in the stdlib. |
Even in situations where it "can't be used" because it would double-check whether the affix matches, it could actually still be used, and is even more performant in many cases by removing the global
For instance, although it's technically redundant, we could still write: if path.endswith('.dylib'):
yield path.removesuffix('.dylib') + suffix + '.dylib'
else:
yield path + suffix While not as clear-cut, it's still a benefit in my opinion. |
@sweeneyde: I don't think that this issue is the right place to discuss that. |
I suggest to move the "can't be used" discussion in https://github.com/python/cpython/pull/19455/files PR ;-) |
The Python Steering Council accepts the PEP 616: |
I'd like to request that PEP 616 be reviewed by the Steering Council.
Title: String methods to remove prefixes and suffixes
Abstract: This is a proposal to add two new methods, removeprefix() and removesuffix(), to the APIs of Python's various string objects. These methods would remove a prefix or suffix (respectively) from a string, if present, and would be added to Unicode str objects, binary bytes and bytearray objects, and collections.UserString.
Links:
Discussion on Python-Dev
GitHub PR
BPO Issue
The text was updated successfully, but these errors were encountered: