On 6/28/2019 10:10 AM, MRAB wrote:
On 2019-06-28 03:40, Glenn Linderman wrote:
On 6/27/2019 3:09 PM, Brett Cannon wrote:
My guess is that without Guido to just ask this will have to go to a PEP as it changes a built-in.
How does adding two new methods change a built-in?

Now if an extra parameter were added to modify lstrip, rstrip, and strip to make them do something different, yes.

But adding new methods doesn't change anything, unless someone is checking for their existence.

My preferred color is  pstrip and sstrip (prefix and suffix strip) since lstrip and rstrip mean left and right.

And maybe there should be a psstrip, that takes two parameters, the prefix and the suffix to strip.

Such functions would certainly reduce code in a lot of places where I do

if  string.startswith('foo'):
     string = string[ 3: ];

as well as making it more robust, because the string and its length have to stay synchronized when changes are made.

lstrip and rstrip remove treat the argument as a set of characters and can remove multiple characters, so having additional methods pstrip and sstrip with their very similar names but different behaviours could be confusing.

I'd prefer to stick with lXXX and rXXX. ldrop and rdrop, maybe?

I was thinking of the change from stripping sets of characters versus stripping specific sequences of characters, all being strip operations.

But when you point out the confusion and suggest the change of names, I got to thinking about names, and really, the   pstrip/ldrop or sstring/rdrop  operations are more like  "replace" than "strip"... there has to be an exact match.

So maybe a better solution to the functionality needed would be lreplace and rreplace.  On the other hand,  the existing   .replace( prefix, '', 1 )  almost implements lreplace! It doesn't require it to be a prefix though :(  And there is no option for doing the last N replacements, nor requiring a single one to be a suffix.  The last N replacements could be done by using a negative N, but it would seem an additional parameter or new name would be required to do prefix- and suffix-restricted operations.

So my example above could be written as either:

string.lreplace('foo')
string.lreplace('foo', '')

But calling the operations lreplace and rreplace, including the option of putting in replacement strings, would actually be quite handy.  Think of this:

filename.rreplace('.html', '.css', True )

where the first parameter is the suffix to remove, the second parameter is a suffix to replace it with, and the third parameter says to add the replacement even if the first parameter is not found/removed.  That's another operation I find frequently in batch file processing, that otherwise takes several lines.

As Ned noted in another branch of this discussion, it would be good to have an update for the 2.7 documentation.  And if new functions are added, by any name, it would be good to have the 3.x documentation clearly reference in both directions between lstrip and lreplace, and between rstrip and rreplace (or whatever names).

_______________________________________________
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/GRPEPJIPGOWEFHZLWWRT3DKGX3XWDSAM/

Reply via email to