如果是直接运行模块的话__name__==‘__main__‘,
通过import 导入模块的话__name__等于模块的名字
test.py
def test(args):
count=args*2
if __name__==‘__main__‘:
print __name__
print "test is :",test(10)
python test.py
输出:
__main__
test is 20
test1.py
import test
print test.__name__
print "import test:",test.test(2)
python test1.py
输出:
test
import test:4
结果可以看出test1是没有输出test is 20的
本文出自 “新掌门” 博客,请务必保留此出处http://linuxnewstar.blog.51cto.com/6967359/1761671
python if __name__ == ' __main__'
原文地址:http://linuxnewstar.blog.51cto.com/6967359/1761671