(no subject)

Michael Chermside mcherm at mcherm.com
Tue May 13 12:12:41 EDT 2003


John J. Lee writes:
> Strictly, I suppose it should raise ValueError if a dict lookup in
> there fails, but part of me wants to just leave it at that and let the
> KeyErrors propagate.  Is that a wicked thought?

I would say that _IF YOU DOCUMENT IT_ there's nothing wrong with
using KeyError for this purpose. Practically, you could use any
kind of error that you liked (re-use a built-in or invent your own)... 
the only danger is that someone might confuse the built-in error
and the one you're raising. Some function might be indexing a list and
also indexing your sorta-dict-like thing also:

    mySortaDictThing.clear_things( myList[0], myList[1] )

In this senario, the user might be confused about whether the
problem was due to myList not having 2 elements, or due to
mySortaDictThing not having the indicated things to clear.

But I really don't see this as being a problem. This is the best
example I can think of, and it's not particularly confusing. I
can't imagine wanting to catch the (naturally occurring) KeyError
and NOT wanting to catch the error from mySortaDictThing. So I'd
say, go for it, and don't feel wicked.

But do document it.

-- Michael Chermside






More information about the Python-list mailing list