On 7/2/2019 12:16 AM, Glenn Linderman wrote:
On 7/1/2019 8:28 PM, Terry Reedy wrote:

I did not read much of the thread, but the proposal is to wrap a near-trivial expression (2 operations) or replace a current method call with a method call than is more or less the same len as what it replaces.

>>> 'prefix_more_suffix'[len('prefix'):]  # if know s begins with p
'_more_suffix'

>>> 'prefix_more_suffix'.replace('prefix', '')
'_more_suffix'

>>> 'prefix_more_suffix'.strip_pre('prefix')  # proposed

Your example isn't equivalent: .replace would replace multiple instances of prefix, not necessarily at the beginning. The .strip_pre (however spelled) would strip one instance, only if it is at the beginning.

I left out the slightly longer regex which does exactly that.

>>> re.sub('^prefix', '', 'prefix_more_suffix')
'_more_suffix'
>>> re.sub('^prefix', '', 'Xprefix_more_suffix')
'Xprefix_more_suffix'

or with minor variation, deletes only suffixes.

>>> re.sub('suffix$', '', 'prefix_more_suffix')
'prefix_more_'

Are these special cases, which can be easily generalized to match more than one string, and to replace rather than delete, special enough to merit a new method each. This remains to be demonstrated.

The more I thought about this, the more I think a more functional goal is a variation of replace that works only on one end or the other, rather than a variation of strip. I outlined that in a different branch in this thread.

To me, a new parameter to str.replace would have a lower bar to acceptance.

The other documentation issue I noticed is that the 2nd and 3rd parameters to startswith and endswith are not fully documented. Typically startswith and endswitch are in the logic prior to the strip/replace operation, and typically only use the first parameter, but looking at their documentation as part of this discussion, I found it lacking.

File an issue?  Preferably with a suggested change.

--
Terry Jan Reedy

_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/DR6US5L6I2NCUSXH5VTWC7DGQHEZ65PE/

Reply via email to