起因
之前再学习python的过程中,在定义函数那一章节发现的问题
原理
一个python文件通常有两种使用方法,第一是作为脚本直接执行,第二是 import 到其他的 python 脚本中被调用(模块重用)执行。因此if name == ‘main’: 的作用就是控制这两种情况执行代码的过程,在if name == ‘main’: 下的代码只有在第一种情况下(即文件作为脚本直接执行)才会被执行,而 import 到其他脚本中是不会被执行的。
不调用演示
print("hello,word")
if __name__ == '__main__':
print('hello,world')
结果
hello,word
hello,world
调用test10.py
import test10
test10
结果
hello,word
这样就一目了然了。。。。。。