据我了解,我可以使用 Python 中的abc模块来创建无法实例化的抽象类(以及其他不错的属性)。我尝试使用它来创建Exception
类层次结构来表示我的应用程序的各种退出代码,但我仍然能够实例化我的基类,即使我不希望这种情况发生。这是一些演示问题的代码:
#!/usr/bin/env python3
import abc
class ExitCodeException(Exception):
__metaclass__ = abc.ABCMeta
def __init__(self, message):
super().__init__()
self._message = message
@abc.abstractmethod
def getExitCode(self):
"""Return the exit code for this exception"""
return
class FatalException(ExitCodeException):
def getExitCode(self):
return 1
raise ExitCodeException("Oh no!")
我期待我的程序退出并出现ExitCodeException
无法实例化的异常,但我只是得到了我期望的标准堆栈跟踪,如果ExitCodeException
不是抽象的:
Traceback (most recent call last)
File "./email2pdf_classexception", line 21, in <module>
raise ExitCodeException("Oh no!")
__main__.ExitCodeException
我怎样才能解决这个问题?