为什么 Python 的 ` except` 不使用 `isinstance` ?

发布于 2024-09-25 22:56:34 字数 1030 浏览 5 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

终遇你 2024-10-02 22:56:34

简单的答案可能是没有人考虑过。一个更复杂的答案是没有人考虑它,因为很难做到这一点,因为这意味着在处理异常时执行潜在的任意 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.

甜是你 2024-10-02 22:56:34

但这有什么原因吗?
不应该实施吗?

你能举个例子吗?

当您执行此操作时:

class SomeMeta(type):
    def __subclasscheck__(cls, sub):
        print (cls, sub)
        return True

class Something(Exception):
    pass

class SomeType(Exception):
    __metaclass__ = SomeMeta

try:
    raise Something()
except SomeType, e:
    pass

# prints (<class '__main__.SomeType'>, <class '__main__.Something'>)

Python 在 SomeType 的元类上调用 __subclasscheck__ 来确定 Something 是否是 SomeType 的子类。 元类 PEP 对此进行了更多讨论细节。

But are there any reasons why this
should not be implemented?

Can you give an example where this would be useful?

When you do this:

class SomeMeta(type):
    def __subclasscheck__(cls, sub):
        print (cls, sub)
        return True

class Something(Exception):
    pass

class SomeType(Exception):
    __metaclass__ = SomeMeta

try:
    raise Something()
except SomeType, e:
    pass

# prints (<class '__main__.SomeType'>, <class '__main__.Something'>)

Python calls __subclasscheck__ on SomeType's metaclass to determine if Something a subclass of SomeType. The Metaclass PEP talks about this in more detail.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文