[Python-Dev] re: string slicing and method consistency
Greg Wilson
Greg.Wilson@baltimore.com
Fri, 20 Apr 2001 17:08:31 -0400
> > Greg Wilson:
> > if "abbc"[-1] is "c", and if
> > "abbc".replace("b", "x", 1) is "axbc", then shouldn't
> > "abbc".replace("b", "x", -1) be "abxc" (i.e. negative
> > numbers replace the *last* occurrences of the value)?
> > Same argument for "split", etc.
> Guido van Rossum:
> Dubious hypergeneralization.
Greg Wilson:
Do you have an editor macro set up yet to generate that
phrase? :-)
> Guido van Rossum:
> The thing is that this parameter,
> called maxsplit, is not really an index -- it's a count.
Greg Wilson:
Understood; I'm asking whether changing its name and
interpretation (in a way that doesn't break any existing
code) would be worthwhile:
>>> path = "/some/long/path/to/file.html"
>>> main, parent, file = path.split("/", -2)
>>> main
"/some/long/path"
>>> parent
"to"
>>> file
"file.html"
> > Greg Wilson:
> > Turns out that "abbc".replace("b", "x", -1) is "axxc"
> > (i.e. negative arguments are ignored). I would have
> > expected this to raise a ValueError, if anything. Is
> > there a reason for this behavior?
Greg Wilson again:
Question still stands --- if these are counts, then shouldn't
negative values raise exceptions?
Thanks,
Greg