为什么 Python 的 ` except` 不使用 `isinstance` ?
Python 文档 for except
说:
对于带有 表达式,该表达式是 评估,并且该子句匹配 如果结果对象是异常 与例外“兼容”。一个 对象与异常兼容 如果它是异常对象的类或基类,[...]
为什么except
不使用isinstance
而不是比较基类?这会阻止使用 __instancecheck__ 来覆盖实例检查。
编辑:
我可以理解,这种情况不存在的原因之一是没有人考虑过它。但有什么理由不应该实施这一点呢?
编辑:
Python 3.2a 的 Shell 会话显示尝试使用 __subclasscheck__
是行不通的:
>>> 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
>>>
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
>>>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
简单的答案可能是没有人考虑过。一个更复杂的答案是没有人考虑它,因为很难做到这一点,因为这意味着在处理异常时执行潜在的任意 Python 代码,并且它的价值值得怀疑。 Python 中的异常类通常是非常简单的类,用功能重载它们通常是一个错误。很难想象让它咨询 __instancecheck__ 的情况。如果您遇到这样的情况(无论是否有补丁),请提交错误,有人可能会发现它。
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.你能举个例子吗?
当您执行此操作时:
Python 在
SomeType
的元类上调用__subclasscheck__
来确定Something
是否是SomeType
的子类。 元类 PEP 对此进行了更多讨论细节。Can you give an example where this would be useful?
When you do this:
Python calls
__subclasscheck__
onSomeType
's metaclass to determine ifSomething
a subclass ofSomeType
. The Metaclass PEP talks about this in more detail.