list index()

Steve Holden steve at holdenweb.com
Wed Sep 5 02:35:56 EDT 2007


TheFlyingDutchman wrote:
>> 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?
> 
> 
Because it's an explicit short-cut for a frequently-required paradigm. 
It's effectively an extension to dict indexing - which, you will recall, 
*does* raise an exception when the key is not found. Does *everything* 
have to raise such an exception?

There's no requirement for Python to be 100% consistent in its 
application of a principle, but on the whole it is reasonably consistent.

For Pete's sake stop wasting your time and go do some programming. 
Nothing will ever be perfect, not even Python ;-)

regards
  Steve
-- 
Steve Holden        +1 571 484 6266   +1 800 494 3119
Holden Web LLC/Ltd           http://www.holdenweb.com
Skype: holdenweb      http://del.icio.us/steve.holden
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------




More information about the Python-list mailing list