Wandering Thoughts archives


Small details can matter (or: a little nifty Python bit)

It's common for Python routines that search for the position of something in a bigger thing (such as characters in a string or list entries in a list) to return -1 when they don't find anything.

I've written Python code for years without really thinking about this. Clearly the routines have to do something when they fail, whether it's raise an exception or return a marker value, and -1 is both simple and not a valid index into an object.

Only recently (while writing some DWiki code) did it occur to me that if what you want is 'everything after where this thing is, or the entire object if it's not there', a return value of -1 is perfect because you can just write:

	pos = thing.searchFor(whatever)
	tail = thing[pos+1:]

If whatever is found in the thing, this clearly works. If it's not found, pos is -1 and the whole expression becomes 'thing[0:]', which in Python is a synonym for all of thing (since Python objects are indexed from zero).

There are certainly other plausible 'not found' marker values, for example None or False. But then this little bit wouldn't work and you'd have to write more code.

There's another clever little property -1 has as an error return: it's considered true in Python, not false. This is useful, because 0 is considered false; that the 'not found' value is not false saves you from writing superficially simple code like:

	pos = someString.find(somechar)
	if not pos:
		# do something on failure

If the 'not found' marker was considered false, this code would work until the day somechar was found at position 0, which might be quite a while. Since it isn't, this code fails immediately and thus gets fixed immediately.

I usually think of the choice of what to return as the 'not found' value as something pretty simple and arbitrary; as long as it's an invalid value and easily checked, it's not something that matters much. But as I've just come to understand, sometimes the small details really can matter, because if they're done right they make your life simpler in little quiet ways.

python/SmallDetailsMatter written at 00:33:46; Add Comment

By day for June 2005: 11 12 14 16 17 18 20 21 22 23 24 26 27 28 29; after June.

Page tools: See As Normal.
Login: Password:
Atom Syndication: Recent Pages, Recent Comments.

This dinky wiki is brought to you by the Insane Hackers Guild, Python sub-branch.