可以在Python中声明一个抽象异常吗? [英] Can one declare an abstract exception in Python?

查看:115
本文介绍了可以在Python中声明一个抽象异常吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Python中声明用户定义的异常的层次结构.但是,我希望我的顶级用户定义类(TransactionException)是抽象的.也就是说,我打算TransactionException指定其子类需要定义的方法.但是,切勿实例化或引发TransactionException.

I would like to declare a hierarchy of user-defined exceptions in Python. However, I would like my top-level user-defined class (TransactionException) to be abstract. That is, I intend TransactionException to specify methods that its subclasses are required to define. However, TransactionException should never be instantiated or raised.

我有以下代码:

from abc import ABCMeta, abstractmethod

class TransactionException(Exception):
    __metaclass__ = ABCMeta

    @abstractmethod
    def displayErrorMessage(self):
        pass

但是,上面的代码允许我实例化TransactionException ...

However, the above code allows me to instantiate TransactionException...

a = TransactionException()

在这种情况下,a是没有意义的,而应该绘制一个异常.以下代码消除了TransactionExceptionException ...

In this case a is meaningless, and should instead draw an exception. The following code removes the fact that TransactionException is a subclass of Exception...

from abc import ABCMeta, abstractmethod

class TransactionException():
    __metaclass__ = ABCMeta

    @abstractmethod
    def displayErrorMessage(self):
        pass

此代码正确地禁止了实例化,但是现在我不能引发TransactionException的子类,因为它不再是Exception.

This code properly prohibits instantiation but now I cannot raise a subclass of TransactionException because it's not an Exception any longer.

可以在Python中定义一个抽象异常吗?如果是这样,怎么办?如果没有,为什么不呢?

Can one define an abstract exception in Python? If so, how? If not, why not?

注意:我使用的是Python 2.7,但很乐意接受Python 2.x或Python 3.x的答案.

NOTE: I'm using Python 2.7, but will happily accept an answer for Python 2.x or Python 3.x.

推荐答案

Alex Martelli在此处.从本质上讲,当存在抽象方法时,归结为各种基本类(objectlist和我假定为Exception)的对象初始化器(__init__)的行为.

There's a great answer on this topic by Alex Martelli here. In essence, it comes down how the object initializers (__init__) of the various base classes (object, list, and, I presume, Exception) behave when abstract methods are present.

当抽象类从object继承时(默认情况下,如果未提供其他基类),则它的__init__方法设置为object的方法,在检查是否已实现所有抽象方法.

When an abstract class inherits from object (which is the default, if no other base class is given), it's __init__ method is set to that of object's, which performs the heavy-lifting in checking if all abstract methods have been implemented.

如果抽象类继承自其他基类,则它将获得该类的__init__方法.其他类,例如listException,似乎不检查抽象方法的实现,这就是为什么允许实例化它们的原因.

If the abstract class inherits from a different base class, it will get that class' __init__ method. Other classes, such as list and Exception, it seems, do not check for abstract method implementation, which is why instantiating them is allowed.

另一个答案为此提供了建议的解决方法.当然,您还有另一种选择,就是简单地接受抽象类是可实例化的,并尝试阻止它.

The other answer provides a suggested workaround for this. Of course, another option that you have is simply to accept that the abstract class will be instantiable, and try to discourage it.

这篇关于可以在Python中声明一个抽象异常吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆