[Python-ideas] str.rreplace
Steven D'Aprano
steve at pearwood.info
Tue Jan 28 13:46:24 CET 2014
On Tue, Jan 28, 2014 at 12:27:31AM -0600, Ron Adam wrote:
> >> 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.)
> >
> >Why does it have to be -1/0/1 instead of just True/False?
>
> Well, then it would need to be.. True/False/None
>
> The reason it needs three modes is to save the current behaviour and not
> break anything.
What's "it", and how is this relevant to adding a version of replace
that operates from the right?
> Actually I'm about even on weather I like the keyword
> option or separate functions.
>
> Also there's the case of taking a slice from the middle with a positive
> starting index and a negative ending index.
Now we're talking about slices?
Providing a positive and negative index to a slice is well-defined and
well-understood operation. "I want everything except the first and last
item" => [1:-1].
> And with the exception of
> examples, nearly all string slicing, use a right and left value to get
> characters in the forward order even if they are indexed from the right.
With the exception of what examples?
The rest of your sentence confuses me. Are you talking about extended
slicing with a negative stride given? Please don't over-generalise this
issue. It's a simple request to add a version of replaces that operates
from the right, just like rfind operates from the right.
> So that gives four modes... left middle right default
> With the default being what we have now.
What?
> I wonder if maybe it would be better to do these things with the string
> format method? That is a higher level interface more suitable for adding
> options to.
You're talking about using a mini-language to control the direction
of a replacement operation. That's not just an over-generalisation, its
a hyper-generalisation.
> >And of course str.find takes negative indices and that's actually used in
> >some quick&dirty scripts:
> >
> > >>> has_ext = path.find('.', -4)
> >
> >Of course you could make an argument that any such scripts deserve to be
> >broken…
>
> I'd say they are already broken in that particular case. ;-)
It's broken, but not because of the negative index.
--
Steven
More information about the Python-ideas
mailing list