On 7/1/2019 8:28 PM, Terry Reedy wrote:
On 7/1/2019 1:57 PM, Chris Barker via Python-Dev wrote:
This was quite extensively discussed on python-ideas recently:
The claim of 'inconsistent results' is based on not reading the doc.
(I'm finding it hard to find a good thread view in the new interface -- but that will get you started)
My memory of that thread is that there was a lot of bike shedding, and quite a lot of resistance to adding a couple new methods, which I personally never understood (Not why we don't want to add methods willy-nilly, but why there was this much resistance to what seems like an low-disruption, low maintenance, and helpful addition)
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
'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.
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.
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.
A method could raise instead of returning the string as-is if the prefix is not really a prefix. How often is this needed? The most common end deletions are whitespace, which the current .strip handles correctly.
raising wouldn't be helpful in most of the situations where I use this logic... it would require a more complex flow control than the if startswith path in the current situation.
Yes, I use strip more frequently, but if startswith: chop_prefix operation (and the other end too) happens an awful lot.