三个python文件怎么关联_python - __name__= __main__怎么解释 怎么用 另外两个py文件之间的互动关联...

name = '__main__' 的作用

有句话经典的概括了这段代码的意义:

“Make a script both importable and executable”

意思就是说让你写的脚本模块既可以导入到别的模块中用,另外该模块自己也可执行。

def foo():

print('in foo')

print(__name__)

foo()

if __name__=='__main__':

foo()

返回结果

main   意思是__name__=__main,所以if语句判断True。

ob05.py

def func():

print("func() in ob05.py")

print("top-level in ob05.py")

if name == "__main__":

print("ob05.py is being run directly")

else:

print("ob05.py is being imported into another module")

结果:

top-level in ob05.py

ob05.py is being run directly

ob06.py

import ob05

print("top-level in ob06.py")

ob05.func()

if name == "__main__":

print("ob06.py is being run directly")

else:

print("ob06.py is being imported into another module")

结果:

top-level in ob05.py

ob05.py is being imported into another module

top-level in ob06.py

func() in ob05.py

ob06.py is being run directly

Thus, when module one gets loaded, its name equals "one" instead of __main__.

意思是ob05模块被导入的话,ob05模块中的__name__=__main__

解释错或不对不完善 麻烦完善下

那import是干嘛用呢,就引入了一个func()?name == "__main__" 是什么意思 起什么作用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值