NameError vs AttributeError
Terry Reedy
tjreedy at udel.edu
Wed Aug 1 12:59:32 EDT 2012
On 8/1/2012 11:53 AM, Ethan Furman wrote:
> Terry Reedy wrote:
>> On 7/31/2012 4:49 PM, Chris Kaynor wrote:
>>> On Tue, Jul 31, 2012 at 1:21 PM, Terry Reedy wrote:
>>>> one wants to catch both errors, one can easily enough. To continue
>>>> the example above, popping an empty list and empty set produce
>>>> IndexError and KeyError respectively:
>>>>
>>>> try:
>>>> while True:
>>>> process(pop())
>>>> except (KeyError, IndexError):
>>>> pass # empty collection means we are done
>>>>
>>> There is a base type for KeyError and IndexError: LookupError.
>>>
>>> http://docs.python.org/library/exceptions.html#exception-hierarchy
>>
>> Oh, so there is. Added in 1.5 strictly as a never-directly-raised base
>> class for the above pair, now also directly raised in codecs.lookup. I
>> have not decided if I want to replace the tuple in the code in my book.
>
> I think I'd stick with the tuple -- LookupError could just as easily
> encompass NameError and AttributeError.
Thank you. Having to remember exactly which lookup error is encompassed
by LookupError illustrates my point about the cost of adding entities
without necessity. It also illustrates the importance of carefull
naming. SubscriptError might have been better.
--
Terry Jan Reedy
More information about the Python-list
mailing list