Why does list.index throw an exception, instead of using an arbitrary value (for example, -1)? What’s the idea behind this?
To me it looks cleaner to deal with special values, rather than exceptions.
EDIT: I didn’t realize -1 is a potentially valid value. Nevertheless, why not something else? How about a value of None?
Because
-1is itself a valid index. It could use a different value, such asNone, but that wouldn’t be useful, which-1can be in other situations (thusstr.find()), and would amount simply to error-checking, which is exactly what exceptions are for.