list index()

TheFlyingDutchman zzbbaadd at aol.com
Wed Sep 5 02:09:20 EDT 2007


>
> I explain it by noting that list.index and dict.get serve totally
> different purposes. The former returns the index given a value; the
> latter returns a value given a key.

And the former raises an exception if the value is not found, while
the latter returns None if the value is not found.

>
> There are many areas of poor symmetry in the language and libraries;
> it isn't particularly clever or difficult to find them if one
> looks. Doing so doesn't appear to illustrate any point I see relevant
> in this thread.

It has been stated in this thread, that in Python, exceptions are used
differently than in other languages. Rather than just use them for
catastrophic errors that can't be dealt with, Python raises exceptions
on routine and survivable situations such as an item not found in a
list. It was mentioned that an exception is more Pythonic than
returning a "not found" value.

Someone pointed out that find() in str was a contradiction to this
since it doesn't throw an exception but returns a "not found" value,
and the response was that it was believed that find() would be removed
from the language (if so, it does not currently show up on the
"removed list" as has_key() and a few others do -
http://docs.python.org/dev/3.0/whatsnew/3.0.html) and was only there
for "historical reasons".

I don't believe the poster was trying to illustrate a point already
mentioned in the thread, but to give a contradiction to statements in
the thread. He was asking the question - why doesn't dict.get() throw
an exception on not found if that is the Pythonic way?





More information about the Python-list mailing list