[Python-ideas] str.rreplace

MRAB python at mrabarnett.plus.com
Tue Feb 4 19:14:33 CET 2014


On 2014-02-04 16:19, Joao S. O. Bueno wrote:
> On 28 January 2014 01:18, Ron Adam <ron3200 at gmail.com> wrote:
>>
>>
>> On 01/24/2014 07:36 PM, Andrew Barnert wrote:
>>>>>>>
>>>>>>> While we're speculatively overgeneralizing, couldn't all of the
>>>>>>> index/find/remove/replace/etc. methods take a negative n to
>>>>>>> count from the end, making r variants unnecessary?
>>>>>
>>>>> Strings already provide rfind and rindex (they're just not part of
>>>>> the general sequence API). Since strings are immutable, there's also
>>>>> no call for an "remove".
>>
>>
>>> I was responding to Serhiy's (probably facetious or devil's advocate)
>>> suggestion that we should regularize the API: add rfind and rindex to
>>> tuple (and presumably Sequence), and those plus rremove to list (and
>>> presumably MutableSequence), and so on.
>>>
>>> My point was that if we're going to be that radical, we might as well
>>> consider removing methods instead of adding them. Some of the find-like
>>> methods already take negative indices; expanding that to all of the
>>> index-based methods, and doing the equivalent to the count-based ones,
>>> and adding a count or index to those that have neither, would mean all
>>> of the "r" variants could go away.
>>
>>
>>
>> How about a keyword to specify which end to index from?  When used, it would
>> disable negative indexing as well.  When not used the current behaviour with
>> negative indexing would be the default.
>>
>>     direction=0            # The default with the current
>>     (or not specified)     #    negative indexing allowed.
>>
>>     direction=1   # From first. Negative indexing disallowed.
>>     direction=-1  # From last.  Negative indexing disallowed.
>>
>> (A shorter key word would be nice, but I can't think of any that is as
>> clear.)
>
>
> I've just picked the whole thread at once -
> and I am a little surprised no one  suggested what looks obvious to me
> (or maybe someone did, I went over the e-mails
> rather quickly):
>
> Why not simply to allow negative indexes to the count argument?
>
> It is pretty much unambiguous, straightforward (hmm..actually, the
> opposite of that) and Pythonistas are used to think of negative
> indices as counting from the right. Moreover, the convention could be
> used for index, remove and even overloaded for split, and other
> methods as well.
>
.split, .rsplit and .replace all currently use -1 to indicate no
maximum.

Oh, and .index and .rindex don't have a count argument! :-)


More information about the Python-ideas mailing list