gpt4 book ai didi

python - 我在哪里可以阅读 import _{module name} in Python?

转载 作者:行者123 更新时间:2023-11-28 22:06:47 25 4
gpt4 key购买 nike

我注意到了这一点。示例:

创建一个名为 ast.py 的空文本文件

$ touch ast.py

运行 Python

$ python
>>> from ast import *
>>> dir()
['__builtins__', '__doc__', '__name__', '__package__']
>>> from _ast import *
>>> dir()
['AST', 'Add', 'And', 'Assert', 'Assign', ...]

ast 是一个 python 模块。那么...这里发生了什么?我尝试使用空的 os.py 但没有成功。

最佳答案

_xxx 模块没有什么特别之处,除了它们是私有(private)(例如_abcoll)或低级(例如 _thread) 并且不打算一般使用。

_ast 模块比较特殊,例如

$ touch _ast.py
$ python -c 'from _ast import *; print(dir())'
['AST', 'Add', 'And', 'Assert', 'Assign', 'Attribute ...

但这不是因为前导_,而是那个_ast是内置模块。 sys 模块也会发生类似的事情。

$ touch sys.py
$ python -c 'from sys import *; print(dir())'
['__builtins__', '__doc__', '__name__', '__package__', 'api_version', 'argv', ...

在 Python 中,_astast两个独立的模块。有一行

from _ast import *

在内置的 ast.py 中,因此导入 ast 也会将 _ast 模块中的所有内容带入。这为您提供了_astast 具有相同内容的错觉,but actually _ast is a lower level module of ast .

关于python - 我在哪里可以阅读 import _{module name} in Python?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3082889/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com