在python开发过程中,应尽量避免导入循环(交叉引用),但是,如果你开发了大型的 Python 工程, 那么你很可能会陷入这样的境地。
一些比较靠谱的方法是:
1. 把 import 语句放到方法定义的后面。
2. 将 import 语句移到函数的内部,只有在执行到这个模块时,才会导入相关模块。
举例:
hello.py
def add(a, b):
return a + b
from world import divs
print divs(110, 11)
world.py
def divs(a, b):
return a / b
from hello import add
print add(1,2)
这样写的打出来的值都会出现循环调用。