[Python-Dev] re: string slicing and method consistency
Greg Wilson
Greg.Wilson@baltimore.com
Sun, 22 Apr 2001 08:19:22 -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.
> > >>> path = "/some/long/path/to/file.html"
> > >>> main, parent, file = path.split("/", -2)
> > >>> main
> > "/some/long/path"
> > >>> parent
> > "to"
> > >>> file
> > "file.html"
> > Guido van Rossum:
> OK, that's an example. It's only so-so, because you should be using
> os.path.split() anyway. It's done best as follows:
>
> temp, file = os.path.split(path)
> main, parent = os.path.split(temp)
Greg Wilson:
Or "main, parent, file = os.path.split(path, -2)" :-)
> > Greg Wilson again:
> > Question still stands --- if these are counts, then shouldn't
> > negative values raise exceptions?
>
> Given that it's documented with the name "maxsplit", it's not
> unreasonable that -1 is treated the same as 0.
Greg Wilson:
But it isn't:
>>> print sys.version
2.2a0 (#2, Apr 20 2001, 12:53:03)
[GCC 2.95.2 19991024 (release)]
>>> "abbc".replace("b", "x", 0)
'abbc'
>>> "abbc".replace("b", "x", -1)
'axxc'
Thanks,
Greg