classification
Title: Proposal to make strip/lstrip/rstrip more explicit
Type: enhancement Stage: resolved
Components: Versions: Python 3.8
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Alex Grigoryev, rhettinger, xtreak
Priority: normal Keywords:

Created on 2019-03-23 20:05 by Alex Grigoryev, last changed 2019-03-23 21:14 by rhettinger. This issue is now closed.

Messages (4)
msg338695 - (view) Author: Alex Grigoryev (Alex Grigoryev) Date: 2019-03-23 20:05
These methods have confusing implicit behavior. I propose to make it explicit, either strip the exact sequence or chars or leave the string as is.

In [1]: 'mailto:maria@gmail.com'.lstrip('mailto')
Out[1]: ':maria@gmail.com'

In [2]: 'mailto:maria@gmail.com'.lstrip('mailto:')
Out[2]: 'ria@gmail.com'

In [3]: 'mailto:maria@gmail.com'.lstrip('ailto:')
Out[3]: 'mailto:maria@gmail.com'
msg338697 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2019-03-23 20:39
https://docs.python.org/3.8/library/stdtypes.html?highlight=lstrip#str.lstrip

> Return a copy of the string with leading characters removed. The chars argument is a string specifying the set of characters to be removed. If omitted or None, the chars argument defaults to removing whitespace. The chars argument is not a prefix; rather, all combinations of its values are stripped:

The last sentence talks about the report. In the given examples it strips all the given characters in chars from left until it finds a character that is not found as part of the given chars argument.

In [2]: 'mailto:maria@gmail.com'.lstrip('mailto:') # Stops at 'r' that doesn't need to be stripped
Out[2]: 'ria@gmail.com'

In [3]: 'mailto:maria@gmail.com'.lstrip('ailto:') # 'm' is the first character and is not found in chars 'ailto:'
Out[3]: 'mailto:maria@gmail.com'

Changing this would break a lot of old code and adding an API for two different behaviors would require a larger discussion. Perhaps did you find any part of docs that you would like to improve to clarify this better?
msg338698 - (view) Author: Alex Grigoryev (Alex Grigoryev) Date: 2019-03-23 20:56
https://docs.python.org/2/library/string.html#string.lstrip (https://link.getmailspring.com/link/4C83E422-2F29-440A-8CE3-0AE8B13F5E87@getmailspring.com/0?redirect=https%3A%2F%2Fdocs.python.org%2F2%2Flibrary%2Fstring.html%23string.lstrip&recipient=cmVwb3J0QGJ1Z3MucHl0aG9uLm9yZw%3D%3D)
Here should be clarified better. Yes I think API for explicit behavior should be discussed, because the other way is this
In [1]: "maria@gmail.com".split("mailto:")[-1]
Out[1]: 'maria@gmail.com'
In [2]: "maria@gmail.commailto:".split("mailto:")[-1]
Out[2]: ''
On март 23 2019, at 10:39 вечера, Karthikeyan Singaravelan <report@bugs.python.org> wrote:
>
> Karthikeyan Singaravelan <tir.karthi@gmail.com> added the comment:
> https://docs.python.org/3.8/library/stdtypes.html?highlight=lstrip#str.lstrip
> > Return a copy of the string with leading characters removed. The chars argument is a string specifying the set of characters to be removed. If omitted or None, the chars argument defaults to removing whitespace. The chars argument is not a prefix; rather, all combinations of its values are stripped:
> The last sentence talks about the report. In the given examples it strips all the given characters in chars from left until it finds a character that is not found as part of the given chars argument.
> In [2]: 'mailto:maria@gmail.com'.lstrip('mailto:') # Stops at 'r' that doesn't need to be stripped
> Out[2]: 'ria@gmail.com'
>
> In [3]: 'mailto:maria@gmail.com'.lstrip('ailto:') # 'm' is the first character and is not found in chars 'ailto:'
> Out[3]: 'mailto:maria@gmail.com'
>
> Changing this would break a lot of old code and adding an API for two different behaviors would require a larger discussion. Perhaps did you find any part of docs that you would like to improve to clarify this better?
> ----------
> nosy: +xtreak
> versions: -Python 3.5, Python 3.6, Python 3.9
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <https://bugs.python.org/issue36410>
> _______________________________________
>
msg338699 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2019-03-23 21:14
Generally, we don't make changes that would break existing code relying on the documented and tested behavior.

If you would like to propose a new method, the python-ideas mailing list would be a good place to start.

>>> s[len('mailto:'):] if s.startswith('mailto:') else s
'maria@gmail.com'
History
Date User Action Args
2019-03-23 21:14:36rhettingersetstatus: open -> closed

type: behavior -> enhancement
versions: - Python 2.7, Python 3.7
nosy: + rhettinger

messages: + msg338699
resolution: not a bug
stage: resolved
2019-03-23 20:56:05Alex Grigoryevsetmessages: + msg338698
2019-03-23 20:39:11xtreaksetnosy: + xtreak

messages: + msg338697
versions: - Python 3.5, Python 3.6, Python 3.9
2019-03-23 20:05:18Alex Grigoryevcreate