[Python-Dev] re: string slicing and method consistency

Guido van Rossum guido@digicool.com
Fri, 20 Apr 2001 17:19:50 -0500


> > Guido van Rossum:
> > Dubious hypergeneralization.
> 
> Greg Wilson:
> Do you have an editor macro set up yet to generate that
> phrase? :-)

No, I actually know how to spell that. :-)

> 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"

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:
> > > 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?

Given that it's documented with the name "maxsplit", it's not
unreasonable that -1 is treated the same as 0.

--Guido van Rossum (home page: http://www.python.org/~guido/)