1

I'm writing a little function to return the index of the first occurrence of a string in a list of strings, using a "fuzzy" comparison.

My question is: what is the proper way to signify the target string not matching any in the source list?

The obvious (only?) thing to do is to return -1. But since -1 in Python means the last element of a sequence, it occurs to me this may not be good Python style. Is there a more Pythonic (Pythonesque?) way?

RFlack
  • 436
  • 1
  • 5
  • 19
  • Thanks all for the several good suggestions. Being new to Python the idea of raising an exception had not occurred to me. The idea of paralleling the index method seems the most Pythonic (Pythonesque?) to me. However I will probably go the None route as I do find peppering ones code with try / exception clauses a bit cumbersome. Once again, many thanks. – RFlack Jan 13 '14 at 04:50

3 Answers3

2

My question is: what is the proper way to signify the target string not matching any in the source list?

You raise an error:

raise ValueError("String Not Found")

Python is a duck typed lnguage; see: http://en.wikipedia.org/wiki/Duck_typing so it's perfectly acceptable and accepted convention to "raise an appropriate error".

Update: As usual, there have been several answers already by now and comments and even suggestions of using raise ValueError. IHMO I believe IndexError to be more appropriate; but this may be a matter of style and personal taste. Also read the: The Zen of Python -- Specifically around the line "There should be one-- and preferably only one --obvious way to do it.".

Update II: I guess for consistency's sake with Pyton's builtin list.index() and str.index() raise ValueError(...) should be used :)

James Mills
  • 18,669
  • 3
  • 49
  • 62
  • @jonrsharpe Thanks :) Is it afterall the nature of the beast (*Python*). – James Mills Jan 12 '14 at 23:53
  • Having said that, [`str.index`](http://docs.python.org/2/library/stdtypes.html#str.index) does use `ValueError`. I guess because the error is with the `sub` argument (it isn't a valid substring to find); no subscript is actually [out of range](http://docs.python.org/2/library/exceptions.html). – jonrsharpe Jan 13 '14 at 00:08
  • @jonrsharpe That is ``True`` :) I'll update my answer for ``ValueError`` since ``list.index()`` and ``str.index()`` both raise this -- again for consistency sake :) I still think it's better to raise ``IndexError`` but oh well :) – James Mills Jan 13 '14 at 00:12
1

You could return None which is the null object.

To check the result you would then use the is operator. Check null object in Python?

Community
  • 1
  • 1
MoRe
  • 1,478
  • 13
  • 25
1

According to the Python docs;

str.rindex; Like rfind() but raises ValueError when the substring sub is not found.

str.rfind(); Return -1 on failure

If you want to keep to the same design as the std libs of Python, given that your function is doing almost the same thing as str.rindex, then raise ValueError(). However if your function is more like rfind, then return -1.

If you don't care about keeping to the same design principles as std libs, then use whatever style you prefer.

See http://docs.python.org/2/library/stdtypes.html#str.count

SleepyCal
  • 5,739
  • 5
  • 33
  • 47