Python中这段代码的意思没看懂,求助

stackoverflow 上关于 What will happen if two modules import each other? 的讨论

https://stackoverflow.com/questions/744373/circular-or-cyclic-imports-in-python

这一段没看懂,哪个是哪个看晕了:

Finally, the executing script runs in a module named __main__, importing the script under its own name will create a new module unrelated to __main__.


例子

# first.py
from second import b

def a(): print(‘This is a’)

# second.py
from first import a

def b():
    print('This is b')


执行 second.py


Python中这段代码的意思没看懂,求助

2 回复

我无法理解你的问题


python some_file.py 的时候,some_file.py 就是 the executing script。

意思是 some_file.py 自己也成为 module,并且这个 module 的名字叫 main,如果 some_file.py 直接或者间接地导入 some_file.py 自己,则被导入的 some_file.py 会成为一个新的 module,且这个新的 module 不是原来的 main

回到顶部