0

I want to create a function that can check if the passed arg is an Exception or one of its subclasses. As an example, I would like the the second is_exception function call to return True as well.

def is_exception(obj):
    return type(obj) == Exception

print is_exception(Exception('asdf'))   => True
print is_exception(EOFError('asdf'))    => False

thanks!

4

1 回答 1

1

您可以使用 issubclass:

>>> issubclass(EOFError,Exception)
True
>>> issubclass(EOFError,EOFerror)
True

签名是“issubclass(A,B)”,如果 A 是 B 的子类,则返回 True,或者“issubclass(A,(B1,B2...))”,如果 A 是任何的子类,则返回 True B1、B2 等

于 2012-04-23T08:32:48.130 回答