Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python导入-解释_Python_Python 3.x_Import - Fatal编程技术网

Python导入-解释

Python导入-解释,python,python-3.x,import,Python,Python 3.x,Import,类似问题: 注意:我正在使用Python 3.3 我在同一个目录中设置了以下两个文件来解释导入,但是我仍然不知道它到底在做什么。我知道函数和类定义是需要运行的语句 untitled.py: import string class testing: def func(self): try: print(string.ascii_lowercase) except: print('not imported')

类似问题:

注意:我正在使用Python 3.3 我在同一个目录中设置了以下两个文件来解释导入,但是我仍然不知道它到底在做什么。我知道函数和类定义是需要运行的语句

untitled.py:

import string

class testing:
    def func(self):
        try:
            print(string.ascii_lowercase)
        except:
            print('not imported')
class second:
    x=1
print('print statement in untitled executed')
stuff.py:

from untitled import testing

try:
    t=testing()
    t.func()
except NameError:
    print('testing not imported')

try:
    print(string.ascii_uppercase)
except NameError:
    print('string not imported')

try:
    print(untitled.string.ascii_uppercase)
except NameError:
    print('string not imported in untitled')

try:
    s=second()
    print(s.x)
except NameError:
    print('second not imported')
print statement in untitled executed
abcdefghijklmnopqrstuvwxyz
string not imported
string not imported in untitled
second not imported
这是我从运行stuff.py获得的输出:

print statement in untitled executed
abcdefghijklmnopqrstuvwxyz
string not imported
string not imported in untitled
second not imported
untitled.py中的print语句将被执行,尽管stuff.py中的import只指定了测试类。此外,stuff.py中字符串模块的关系是什么,因为它可以从测试类内部调用,但不能从外部调用

有人能给我解释一下这种行为吗,“from import”语句到底做什么(它运行什么)?

该语句在加载行为方面与
import
没有任何不同。加载模块时,始终执行顶层代码
from
仅控制加载模块的哪些部分被添加到当前范围(第一点最重要):

from表单使用稍微复杂一些的过程:

  • 查找from子句中指定的模块,并在必要时对其进行初始化
  • 对于导入条款中指定的每个标识符:
    • 检查导入的模块是否具有该名称的属性
    • 如果没有,请尝试导入具有该名称的子模块,然后再次检查导入的模块是否具有该属性
    • 如果未找到该属性,则引发ImportError
    • 否则,将在本地命名空间中绑定对该值的引用,如果存在,则使用as子句中的名称,否则使用属性名称
因此,您可以使用以下不雅观的技巧访问部分使用
导入的模块的内容:

print(sys.modules['untitled'].string.ascii_uppercase)

您可以将python模块视为名称空间。请记住,导入不包括:

  • 模块只导入一次
  • 第一次执行顶层代码
  • 任何导入、变量、函数或类声明都只影响模块本地名称空间
假设您有一个名为
foo.py
的模块:

import eggs

bar = "Lets drink, it's a bar'
因此,当您在另一个模块中从foo import bar执行
时,您将使
bar
在当前名称空间中可用。如果您执行
导入foo
,则模块
eggs
将在
foo.eggs
下可用。如果从foo import*
执行
,那么
eggs
bar
以及模块名称空间中的所有其他内容也将位于当前名称空间中-但决不要这样做,Python中不支持通配符导入

如果执行
import foo
,然后执行
import eggs
,则
eggs
处的顶级代码将执行一次,模块名称空间将存储在模块缓存中:如果另一个模块导入该名称空间,则信息将从此缓存中提取。如果您要使用它,那么导入它-无需担心多次导入会多次执行顶级代码

Python程序员非常喜欢名称空间;我总是尝试使用
import foo
,然后使用
foo.bar
,而不是从foo import bar
中使用
,如果可能的话-它可以保持名称空间干净并防止名称冲突

这就是说,导入机制是可以破解的,您可以使python
import
语句工作,即使文件不是python。

在您的第一个文件(untitled.py)中,当python编译器解析(因为您在import中调用它)该文件时,它将创建2个类代码对象并执行print语句。请注意,如果从命令行运行untitled.py,它甚至会打印它

在第二个文件(stuff.py)中,要添加到@Paulo comments中,您在namspace中只导入了测试类,因此只有该类可以从untitle.py中的2个代码对象中使用 但是如果你只是说

import untitled
您的第三个“try”语句将起作用,因为它的名称空间中将没有标题


下一件事。尝试导入untitled.testing:)

根据您的说法,我知道From foo import bar等同于导入foo;bar=foo.bar'?如果是这种情况,为什么“print(untitled.string.ascii_uppercase)”不能在stuff.py(或untitled.second)中工作@Michal,这是不等价的。它在“加载模块”方面是等效的,即执行顶层代码。上面明确指出了不同之处,即仅将指定的名称添加到本地范围。是否有一种方法可以在不更改任何导入的情况下从stuff访问字符串模块?这意味着所有模块foo都已运行,但只有bar被添加到当前文件的命名空间中?是的,只有
bar
被添加到命名空间中;请注意,
foo
处的顶级代码只运行一次,因此,如果它以前由另一个模块导入,它将不会再次运行,而是从模块缓存中提取
bar