Python 2 的"异常"模块在 Python3 中缺失,它的内容去哪儿了?



一位朋友在Python 2中提到过(假设你在命令行上的路径环境变量中拥有它)

$ pydoc exceptions 

非常有用,并且知道它应该每周为他节省几分钟的网络查找时间。我自己每周谷歌一次异常层次结构,所以这对我来说也是一个有用的提醒。它与您获得的文档相同

>>> import exceptions
>>> help(exceptions) 

在 Python 2 中,因为pydoc使用异常模块来提供在线文档。

但是,他指出这不适用于Python 3。这是因为 Python 3 中不存在 exceptions 模块。

我可以看到他为什么喜欢它 - 它显示了非常有用的异常层次结构以便快速阅读,我自己经常引用它。但是 Python 3 中缺少带有生成的内置文档的 exceptions 模块!他怎么能取代它?

为了确保 Stackoverflow 有这个问题的答案,一般来说:

迁移到 Python 3 时,如何替换 Python 2 中异常模块的内容?

作为前提性评论,让我说在大多数情况下,你不需要 Python 2 的 exceptions 模块的内容,因为它们可以在所有模块的__builtin__全局命名空间中找到。但是,我们希望将其用于在线文档。

在这种情况下,简单的答案是,为了保持一致性,Python 2 的 exceptions 模块的内容已移动到builtins模块。

在 Python 3 shell 中:

>>> import builtins
>>> help(builtins)

将提供相同的文档。

如果你的路径上有Python 3的目录(也就是说,你可以在命令行上键入python,它会弹出Python 3 shell),那么用

$ pydoc builtins

我们会得到相同的结果。

如果你想测试这个,但你的路径上没有 Python 3 的 pydoc,你可以在你的 Python3.x 目录中测试它,使用以下两个,我得到了相同的输出:

$ python3 pydoc.py builtins
$ ./pydoc.py builtins

您将看到 Python 3 的异常层次结构(如下所示),以及文档的其余部分:

    BaseException
        Exception
            ArithmeticError
                FloatingPointError
                OverflowError
                ZeroDivisionError
            AssertionError
            AttributeError
            BufferError
            EOFError
            ImportError
            LookupError
                IndexError
                KeyError
            MemoryError
            NameError
                UnboundLocalError
            OSError
                BlockingIOError
                ChildProcessError
                ConnectionError
                    BrokenPipeError
                    ConnectionAbortedError
                    ConnectionRefusedError
                    ConnectionResetError
                FileExistsError
                FileNotFoundError
                InterruptedError
                IsADirectoryError
                NotADirectoryError
                PermissionError
                ProcessLookupError
                TimeoutError
            ReferenceError
            RuntimeError
                NotImplementedError
            StopIteration
            SyntaxError
                IndentationError
                    TabError
            SystemError
            TypeError
            ValueError
                UnicodeError
                    UnicodeDecodeError
                    UnicodeEncodeError
                    UnicodeTranslateError
            Warning
                BytesWarning
                DeprecationWarning
                FutureWarning
                ImportWarning
                PendingDeprecationWarning
                ResourceWarning
                RuntimeWarning
                SyntaxWarning
                UnicodeWarning
                UserWarning
        GeneratorExit
        KeyboardInterrupt
        SystemExit

一位评论者说:

包含python 2/3兼容性解决方案会很好。我的用例是语法突出显示器的所有异常名称的列表。

为了兼容性,我会做这样的事情:

try:
    import exceptions
except ImportError:
    import builtins as exceptions
exceptions_list = sorted(n for n, e in vars(exceptions).items() 
                         if isinstance(e, type) and 
                            issubclass(e, BaseException))

你可以期望builtins在Python 3中拥有每个内置异常,就像exceptions在Python 2中所做的那样 - 它也会有其余的内置异常。

exceptions_list可以是所有内置异常的规范列表。

最新更新