Having x.split(y)
always return a list of 1 + x.count(y)
items is a precious regularity -- as @gnibbler's already pointed out it makes split
and join
exact inverses of each other (as they obviously should be), it also precisely maps the semantics of all kinds of delimiter-joined records (such as csv
file lines [[net of quoting issues]], lines from /etc/group
in Unix, and so on), it allows (as @Roman's answer mentioned) easy checks for (e.g.) absolute vs relative paths (in file paths and URLs), and so forth.
Another way to look at it is that you shouldn't wantonly toss information out of the window for no gain. What would be gained in making x.split(y)
equivalent to x.strip(y).split(y)
? Nothing, of course -- it's easy to use the second form when that's what you mean, but if the first form was arbitrarily deemed to mean the second one, you'd have lot of work to do when you do want the first one (which is far from rare, as the previous paragraph points out).
But really, thinking in terms of mathematical regularity is the simplest and most general way you can teach yourself to design passable APIs. To take a different example, it's very important that for any valid x
and y
x == x[:y] + x[y:]
-- which immediately indicates why one extreme of a slicing should be excluded. The simpler the invariant assertion you can formulate, the likelier it is that the resulting semantics are what you need in real life uses -- part of the mystical fact that maths is very useful in dealing with the universe.
Try formulating the invariant for a split
dialect in which leading and trailing delimiters are special-cased... counter-example: string methods such as isspace
are not maximally simple -- x.isspace()
is equivalent to x and all(c in string.whitespace for c in x)
-- that silly leading x and
is why you so often find yourself coding not x or x.isspace()
, to get back to the simplicity which should have been designed into the is...
string methods (whereby an empty string "is" anything you want -- contrary to man-in-the-street horse-sense, maybe [[empty sets, like zero &c, have always confused most people;-)]], but fully conforming to obvious well-refined mathematical common-sense!-).