zl程序教程

您现在的位置是:首页 >  后端

当前栏目

90 python高级 - 循环导入

Python循环导入 高级 90
2023-09-11 14:15:43 时间

1. 什么是循环导入

a.py

from b import b 

print '---------this is module a.py----------'
def a():
    print("hello, a")
    b() 

a()

b.py

from a import a

print '----------this is module b.py----------'
def b():
    print("hello, b")

def c():
    a() 
c()

运行python a.py
在这里插入图片描述

2. 怎样避免循环导入

  1. 程序设计上分层,降低耦合
  2. 导入语句放在后面需要导入时再导入,例如放在函数体内导入