Python 2 的 `exceptions` 模块在 Python3 中丢失了,它的内容到哪里去了?

2024-05-16

一位朋友提到,对于 Python 2,(假设您在命令行上的路径环境变量中有它)

$ pydoc exceptions 

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

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

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

然而,他指出这不适用于 Python 3。这是因为exceptionsPython 3 中不存在该模块。

我明白他为什么喜欢它 - 它显示了非常有用的异常层次结构,可以快速阅读,我自己也经常引用它。但是exceptionsPython 3 中缺少包含生成的内置文档的模块!他怎样才能取代呢?

为了确保 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 中的所有内置异常,就像exceptionsPython 2 中就是这样做的 - 它也将具有其余的内置函数。

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Python 2 的 `exceptions` 模块在 Python3 中丢失了,它的内容到哪里去了? 的相关文章

随机推荐