Python3中缺少Python

Python3中缺少Python

本文介绍了Python3中缺少Python 2的“ exceptions”模块,其内容去了哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

A friend mentioned that with Python 2, (assuming you have it on your path environment variable, on the commandline)

$ pydoc exceptions

非常有用,并且知道应该每周为他节省几分钟的网络查找时间。我本人每周大约一次地在Google例外层次结构中进行搜索,因此这对我也很有帮助。

is very useful and knowing it should save him a few minutes of web lookup time a week. I Google the exceptions hierarchy about once a week myself, so this was a helpful reminder for me as well. It is the same documentation that you get with

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

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

in Python 2, because pydoc uses the exceptions module to provide the online documentation.

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

However, he noted this doesn't work with Python 3. This is because the exceptions module doesn't exist in Python 3.

我明白他为什么喜欢它-它显示了非常有用的异常层次结构,可以快速浏览,而我本人也经常引用它。但是Python 3缺少带有生成的内置文档的 exceptions 模块!他如何替换它?

I can see why he likes it - it shows the very useful exceptions hierarchy for quick perusal, and I reference it myself frequently. But the exceptions module with the resulting builtin documentation is missing from Python 3! How can he replace it?

通常,为了确保Stackoverflow可以回答此问题,

To ensure that Stackoverflow has the answer to this question, in general:


推荐答案

首先,我要说的是,在大多数情况下,您不需要Python 2的 exceptions 模块,如在所有模块的 __ builtin __ 全局命名空间中所见。但是,我们需要在线文档。

As a prefatory remark, let me say that in most cases, you don't need the contents of Python 2's exceptions module, as they are found in the __builtin__ global namespace in all modules. However, we want it for the online documentation.

在这种情况下,简单的答案是Python 2的 exceptions 模块移至 builtins 模块。

In this case, the simple answer is that the contents of Python 2's exceptions module has been moved, for consistency, to the builtins module.

在Python 3 shell中:

In a Python 3 shell:

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

将提供相同的文档。

并且如果您的路径上有Python 3的目录(也就是说,您可以在命令行上键入python,它会显示Python 3 shell),然后使用

And if you have Python 3's directory on your path (that is, you can type python on your command line and it brings up the Python 3 shell) then with

$ pydoc builtins

我们将得到相同的结果。

We'll get the same.

如果您想进行测试,但是路径上没有Python 3的pydoc,则可以在Python3.x目录中使用以下两项进行测试,我得到了相同的输出:

If you want to test this, but don't have Python 3's pydoc on your path, you can test it in your Python3.x directory with both of the following, I got the same output:

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

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

And you'll see Python 3's exception hierarchy (shown below), along with the rest of the documentation:

    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

评论者说:

出于兼容性考虑,我会这样做:

I would do something like this for compatibility:

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中做到了-它也将具有其余的内置程序。

You could expect builtins to have every built-in exception in Python 3, just as exceptions did in Python 2 - it would just also have the rest of the builtins as well.

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

The exceptions_list could be your canonical list of all builtin exceptions.

这篇关于Python3中缺少Python 2的“ exceptions”模块,其内容去了哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 10:14
查看更多