Python导入语句是否也会自动导入依赖项? [英] Does Python import statement also import dependencies automatically?

查看:130
本文介绍了Python导入语句是否也会自动导入依赖项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下文件 app.py

  class Baz ():
def __init __(self,num):
self.a = num
print self.a

def foo(num):
obj = Baz(num)

和第二个文件 main.py

  from app import foo 
foo(10)
> python main.py
给出正确的输出。

$

b
$ b

现在在第二个文件中,我只是导入了函数而不是类,尽管我的函数的成功执行也需要类。



当导入函数时,Python会自动导入运行该函数所需的所有内容,还是自动搜索当前目录中的类?

解决方案

正如@DavidZ已经提到的那样,当我们导入它时,整个Python文件被编译。但是另一件特别的事情发生在函数体被解析时,函数知道它应该在本地作用域中查找哪些变量以及它应该在全局作用域中寻找哪些变量(以及有自由变量)。

 >>> import dis 
>>> dis.dis(foo)
7 0 LOAD_GLOBAL 0(Baz)
3 LOAD_FAST 0(num)
6 CALL_FUNCTION 1
9 STORE_FAST 1(obj)
12 LOAD_CONST 0(无)
15 RETURN_VALUE

所以,这里 Baz 必须从全局范围中获取。


但是,如何在另一个导入app.py时识别全局范围
文件?

那么,每个函数都有一个特殊的属性 __ globals __ 附加到它包含其实际的全局名称空间。因此,这就是 Baz 的来源:

 >>> ; foo .__ globals __ ['Baz'] 

因此,app的模块字典和 foo .__全局变量__ 指向同一个对象:

 >>> sys.modules ['app'] .__ dict__ is foo。 __globals__ 
True

即使您定义了另一个名为 Baz > main.py 之后导入 foo 它仍然会访问实际的 Baz



data-model page

__ globals__ func_globals p>


对保存函数全局
变量的字典的引用 - 模块中的全局名称空间,其中函数
被定义。



I have the following file app.py

class Baz():
    def __init__(self, num):
        self.a = num
        print self.a

def foo(num):
    obj = Baz(num)

and the second file main.py

from app import foo
foo(10)

Running the file python main.py gives the correct output.

Now in the second file, I'm just importing the function not the class, although successful execution of my function requires the class as well.

When importing the function does Python automatically import everything else which is needed to run that function, or does it automatically search for the class in the current directory?

解决方案

As @DavidZ already mentioned the whole Python file gets compiled when we import it. But another special thing happens when a function body is parsed, a function knows which variables it should look for in local scope and which variables it should look for in global scope(well there are free variables too).

>>> import dis
>>> dis.dis(foo)
  7           0 LOAD_GLOBAL              0 (Baz)
              3 LOAD_FAST                0 (num)
              6 CALL_FUNCTION            1
              9 STORE_FAST               1 (obj)
             12 LOAD_CONST               0 (None)
             15 RETURN_VALUE

So, here Baz must be fetched from global scope.

But how to identify this global scope when we import app.py in another file?

Well, each function has a special attribute __globals__ attached to it which contains its actual global namespace. Hence, that's the source of Baz:

>>> foo.__globals__['Baz']
<class __main__.Baz at 0x10f6e1c80>

So, app's module dictionary and foo.__globals__ point to the same object:

>>>sys.modules['app'].__dict__ is foo.__globals__ 
True

Due to this even if you define another variable named Baz in main.py after importing foo it will still access the actual Baz.

From data-model page:

__globals__ func_globals:

A reference to the dictionary that holds the function’s global variables — the global namespace of the module in which the function was defined.

这篇关于Python导入语句是否也会自动导入依赖项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆