Just remember that Python is sexy

Duncan Booth duncan.booth at invalid.invalid
Thu May 26 04:25:44 EDT 2005


Ville Vainio wrote:

>>>>>> "Peter" == Peter Hansen <peter at engcorp.com> writes:
> 
>     Peter> Sion Arrowsmith wrote:
>     >> But can you come up with a method for remembering which way
>     >> round str.find() and str.index() are?
> 
>     Peter> Don't use "str" and you won't have anything to remember:
> 
>     Peter> 'foo bar baz'.find('spam')
>     Peter> 'spanish inquisition'.index('parrot')
> 
> But which one raises an exception, and which one returns -1?
> 
The name index implies it returns something you can use as an index to get 
at the substring.

In other words you can always assume that the following expression is True 
(if it has any value at all) when you use the result of index as an index:

    s[s.index(t):].startswith(t)

The name 'find' doesn't really imply anything about the result, so it can 
return pretty much any crud such as -1.



More information about the Python-list mailing list