python 模块之间相互调用(import)

项目目录结构为:
before
├── main.py
└── test_code
├── init.py
├── test_cod.py
├── bb.py
└──cc.py
├── init.py
└── demo.py

test_cod.py

# from before.test_code import cc
import cc
import bb
print("this is test_cod")

bb.py

import test_cod
print("this is bb")

cc.py

import test_cod
print("this is cc")

运行test_cod.py,结果如下

this is bb
this is test_cod
this is cc
this is test_cod
                                       |-import cc (已执行,不在执行)
                                       |-import bb-|-import test_cod(已执行,不在执行)
                                       |           |-print("this is bb") 第一次打印
                     |-import test_cod-|-("this is test_cod") 第二次打印
                     |-print("this is cc") 第三次打印
         |-import cc-|
test_cod-|-import bb--(已执行,不在执行)--------|---import test_cod
         |-print("this is test_cod") 第四次打印 |---print("this is bb")

test_cod.py里代码的顺序是import cc再import bb,然后print(“this is test_cod”),
先import cc(即执行cc.py,第一次import test_cod (执行test_cod.py(这次是从import bb(第二次import test_cod,这次不会import,直接打印"this is bb",跳出)开始执行,然后打印"this is test_cod"),然后打印"this is cc"),因为bb已经被导入过一次,不在执行import bb,最后执行print(“this is test_cod”)。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值