码迷,mamicode.com
首页 > 编程语言 > 详细

python if __name__ == ' __main__'

时间:2016-04-08 15:31:26      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:python   __name__   __main__   

如果是直接运行模块的话__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__'

标签:python   __name__   __main__   

原文地址:http://linuxnewstar.blog.51cto.com/6967359/1761671

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!