首先可以用一句话概括:if __name__ == __main__语句的意义是为了使当前脚本可以正常执行,在被其他脚本调用时也可以执行
举个栗子: print.py 文件中的代码如下:
print “the first”
if __name__ == __main__:
print “the second”
如果执行当前脚本,则执行结果为 the first the second
如果是其他脚本调用执行 则执行结果为 the first
总结就是:在执行当前脚本时 if __name__ == __main__ 结果为true 所以会执行 if之后的语句
在被其他脚本调用时,__name__为当前文件的名字 所以 if 之后的语句不会执行