NameError vs AttributeError

Chris Kaynor ckaynor at zindagigames.com
Tue Jul 31 16:49:25 EDT 2012


On Tue, Jul 31, 2012 at 1:21 PM, Terry Reedy <tjreedy at udel.edu> wrote:

> Another example: KeyError and IndexError are both subscript errors, but
> there is no SubscriptError superclass, even though both work thru the same
> mechanism -- __getitem__.  The reason is that there is no need for one. In
> 'x[y]', x is usually intented to be either a sequence or mapping, but not
> possibly both. In the rare cases when 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


>
>
> --
> Terry Jan Reedy
>
>
>
> --
> http://mail.python.org/**mailman/listinfo/python-list<http://mail.python.org/mailman/listinfo/python-list>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20120731/4dd8414f/attachment.html>


More information about the Python-list mailing list