try... except with unknown error types

Peter Otten __peter__ at web.de
Sat Sep 10 06:17:23 EDT 2011


Chris Torek wrote:

> >>> import socket
> >>> isinstance(socket.error, IOError)
> False

Here you test if the socket.error *class* is an instance of IOError; this 
would print True if IOError were socket.error's metaclass. However:

>>> isinstance(socket.error(), IOError)
True

or more directly:

>>> issubclass(socket.error, IOError)
True
>>> issubclass(socket.error, EnvironmentError)
True

This is a relatively recent change:

$ python2.5 -c'from socket import error; print issubclass(error, IOError), 
issubclass(error, EnvironmentError)'
False False
$ python2.6 -c'from socket import error; print issubclass(error, IOError), 
issubclass(error, EnvironmentError)'
True True




More information about the Python-list mailing list