在Python3中,“导入”是否可以传递工作?

2 python import programming-languages

在Python3中,import 可以过渡工作吗?

例如,如果一个模块包含import A,并且该模块A包含import B,则不会模块导入B间接?

与其他语言相比:

  • 在Java中,有些人说import不能暂时使用,请参阅/sf/answers/3267436511/

  • 在C语言中,include确实可以传递。例如,如果一个文件包含#include "A.h",并且A.h包含#include "B.h",则该文件也将B.h间接包含。

Python import和Java的import, and C'sinclude` 如何在工作上有区别?

谢谢。

Che*_* A. 5

将模块导入名称空间时,Python会创建模块名称空间。这是递归的;导入时A,它将导入B,如果导入失败,则会出现错误。否则可以通过以下方式访问A.B

# temp.py
def func_in_b():
    print 'this is B'

# temp2.py
import temp

def func_in_a():
    print 'this is A'

>>> import temp2
>>> temp2.func_in_a()
this is A
>>> temp2.temp.func_in_b()
this is B
Run Code Online (Sandbox Code Playgroud)