创建一个文件,
a.py公司
:
print(__name__) # It will print out __main__
__name__
总是等于
__main__
无论什么时候
直接运行
显示这是主文件。
创建另一个文件,
b.py公司
,在同一目录中:
import a # Prints a
运行它它会打印出来
一
,即
是进口的
.
所以,为了显示
同一文件的两种不同行为
,这是一个常用的技巧:
# Code to be run when imported into another python file
if __name__ == '__main__':
# Code to be run only when run directly