Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't Python's `except` use `isinstance`?

The Python documentation for except says:

For an except clause with an expression, that expression is evaluated, and the clause matches the exception if the resulting object is “compatible” with the exception. An object is compatible with an exception if it is the class or a base class of the exception object, [...]

Why doesn't except use isinstance instead of comparing base classes? This is preventing the use of __instancecheck__ to override the instance check.

EDIT:

I can understand that one of the reasons this doesn't exist is that no one considered it. But are there any reasons why this should not be implemented?

EDIT:

Shell session from Python 3.2a showing that trying to use __subclasscheck__ for this doesn't work:

>>> class MyType(type): __subclasscheck__ = lambda cls, other_cls: True
>>> class O(Exception, metaclass=MyType): pass
>>> issubclass(3, O)
0: True
>>> issubclass(int, O)
1: True
>>> try:
...     1/0
... except O:
...     print('Success')
Traceback (most recent call last):
  File "<pyshell#4>", line 2, in <module>
    1/0
ZeroDivisionError: division by zero
>>> 
like image 875
Ram Rachum Avatar asked Oct 01 '10 22:10

Ram Rachum


People also ask

What can I use instead of Isinstance in Python?

We could use type(variable) == str instead. It would work, but it's a bad idea: == should be used when you want to check the value of a variable.

Should you use Isinstance Python?

If you need to check the type of an object, it is recommended to use the Python isinstance() function instead. It's because isinstance() function also checks if the given object is an instance of the subclass.

Is Isinstance the best way to check types?

Generally speaking isinstance is a 'more' elegant way of checking if an object is of a certain "type" (since you are aware of the Inheritance chain). On the other hand, if you are not aware of the inheritance chain and you need to be pick, go for type(x) == ...

What is the difference between the type () and Isinstance () in Python?

Python : Difference between type() and isinstance() type() simply returns the type of an object. Whereas, isinstance(): returns true if the object argument is an instance of the classinfo argument, or of a (direct, indirect or virtual) subclass thereof.


1 Answers

The simple answer is probably that nobody considered it. A more complex answer would be that nobody considered it because it's hard to get this right, because it would mean executing potentially arbitrary Python code while handling an exception, and that it is of dubious value. Exception classes in Python are typically very simple classes, and overloading them with functionality is often a mistake. It's hard to imagine a case for having it consult __instancecheck__. If you have such a case (with or without a patch), file a bug and someone might pick it up.

like image 187
Thomas Wouters Avatar answered Sep 18 '22 13:09

Thomas Wouters