gpt4 book ai didi

python - 在python 3中导入,请解释输出

转载 作者:行者123 更新时间:2023-12-04 14:22:01 25 4
gpt4 key购买 nike

内容 :

tutorials/maindir/
├── dir1
│   ├── file11.py
│   ├── file12.py
│   ├── __init__.py
│   └── __pycache__
│   ├── file11.cpython-36.pyc
│   └── __init__.cpython-36.pyc
├── dir2
│   ├── file21.py
│   ├── file22.py
│   └── __init__.py
├── file1.py
├── file2.py
├── __init__.py
└── __pycache__
├── file1.cpython-36.pyc
└── __init__.cpython-36.pyc

现在我有一个代码给我以下我没有得到的输出。

代码::
print("--main--", dir())
from tutorials.maindir import *
print("--main--", dir())

输出::
--main-- ['__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__']
tutorials
tutorials.maindir
tutorials.maindir.dir1
--main-- ['__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'dir1', 'tutorials']

我不明白为什么 dir1tutorials有没有在 dir()的输出。
maindir.__init__.py的代码:
print(__name__)
import tutorials.maindir.dir1.file11

编辑 1::

所以如果代码是:
print("--main--", dir())
import tutorials.maindir.dir1.file11
print("--main--", dir())

输出是:
--main-- ['__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__']
tutorials
tutorials.maindir
tutorials.maindir.dir1
--main-- ['__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'tutorials']

最佳答案

当您运行 import你正在造成__init__.py运行。 [ source ]

If __all__ is not defined, the statement from sound.effects import * does not import all submodules from the package sound.effects into the current namespace; it only ensures that the package sound.effects has been imported (possibly running any initialization code in __init__.py) and then imports whatever names are defined in the package. This includes any names defined (and submodules explicitly loaded) by __init__.py. It also includes any submodules of the package that were explicitly loaded by previous import statements.



在您的情况下,这包括另一个 import :
import tutorials.maindir.dir1.file11

这会导致某些东西被绑定(bind)到本地命名空间:[ source ]

define a name or names in the local namespace for the scope where the import statement occurs.



更具体地说:

If the module being imported is not a top level module, then the name of the top level package that contains the module is bound in the local namespace as a reference to the top level package. The imported module must be accessed using its full qualified name rather than directly



所以在你的情况下, tutorials是已经导入的顶层模块, dir1是导入的包(通过父名称选择)。

最后是 dir()将前往: [ source ]

Without arguments, return the list of names in the current local scope. With an argument, attempt to return a list of valid attributes for that object.



因此,根据您的评论,导入语句中定义的顶级包和各个名称绑定(bind)到本地命名空间,这就是它们出现的原因,但 maindir 不是顶级包或导入的包,因此它不会显示。

编辑: (和其他信息)

首先,如果所有这些对您来说都非常有趣或相关,我建议您对一些事情进行一些技术阅读:
  • PEP 420 -- 隐式命名空间包
  • PEP 302 -- 新进口 Hook
  • PEP 328 -- 导入:多行和绝对/相对

  • 这些 PEP 讨论了很多现代导入的工作原理以及它的一些特殊和奇怪的部分,以及如何形成它以在您的用例中执行您需要它执行的操作。

    关于您的编辑问题:

    When the name variable is of the form package.module, normally, the top-level package (the name up till the first dot) is returned, not the module named by name. However, when a non-empty fromlist argument is given, the module named by name is returned. [source]



    因此,对于您的基本长导入语句,它将把顶级模块放入命名空间。

    If the list of identifiers is replaced by a star ('*'), all public names defined in the module are bound in the local namespace for the scope where the import statement occurs. [source]


    import name给你名字 import name.one给你名字 import name.one.two给你名字 from name import one给你一个 from name import one as two给你两个

    现在回到你的例子: from tutorials.maindir import *将首先导入顶级模块。然后 __init__.py将运行另一个导入。该导入将跳过顶层模块,因为它已经导入,并将向下移动到包,然后作为父级的名称导入,或者本质上是最右边的 .它将向左移动一个术语并作为该名称导入。 [ code ]。

    关于python - 在python 3中导入,请解释输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57903867/

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