Python3 中缺少 Python 2's ` exceptions` 模块,它的内容去哪儿了?

标签 python python-2.7 exception python-3.x built-in

一位 friend 提到使用 Python 2,(假设您在命令行的路径环境变量中有它)

$ pydoc exceptions 

非常有用,并且知道它每周可以节省几分钟的网页查找时间。我自己大约每周一次谷歌异常层次结构,所以这对我来说也是一个有用的提醒。它与您使用

获得的文档相同
>>> import exceptions
>>> help(exceptions) 

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

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

我明白他喜欢它的原因 - 它显示了非常有用的异常层次结构,便于快速阅读,我自己也经常引用它。但是 Python 3 中缺少包含生成的内置文档的 exceptions 模块!他怎么能代替呢?

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

How does one replace the contents of the exceptions module in Python 2 when moving to Python 3?

最佳答案

作为序言,让我说在大多数情况下,您不需要 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

一位评论者说:

Would be nice to include a python 2/3 compatibility solution. My use case was a list of all exception names for a syntax highlighter.

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

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 中的每个内置异常,就像 Python 2 中的 exceptions 一样 - 它也将拥有其余的内置

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

关于Python3 中缺少 Python 2's ` exceptions` 模块,它的内容去哪儿了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27030933/

相关文章:

c# - .NET 3.5 C# 错误与 System.Timer System.ObjectDisposedException : Cannot access a disposed object

python - 为什么 Open3D 法线在 x 方向上不正确?

Python:使用追加将列表中的字符串值更改为 ascii 值

python - 如何打开包含 Unicode 字符的 html 文件?

Python树结构字典创建问题

php - 绕过 mysqli_connect() 错误编写单元测试

python - 将 to_datetime 应用于除索引之外的所有列

python - 如何编写一个每次运行都给出相同输出的 Python 程序?

python - 删除包含任何数字子字符串的列行

c# - 登录页面和MySqlDataReader异常错误