标签:python __name__=='__main__' 用法
python 文件的后缀为.py,比如 name.py
python 文件可以直接执行,也可以被导入、调用,比如import name;
script 1:
#!/bin/python # Filename:name.py if __name__==‘__main__‘: print ‘This program is being run by itself‘ else: print ‘I am being imported from another module‘
script 2:
#!/bin/python # filename:test.py import name;
执行 test.py 返回 C:\Python27\python.exe C:/Users/Administrator/PycharmProjects/11/test.py I am being imported from another module Process finished with exit code 0
结论:
__name__==‘__main__‘ 代表直接执行
如果被当作模块导入到其他python文件中,执行else 后面的命令
一般我们在调试的时候会用到
本文出自 “roidba” 博客,请务必保留此出处http://roidba.blog.51cto.com/12318731/1894285
python __name__=='__main__' 用法
标签:python __name__=='__main__' 用法
原文地址:http://roidba.blog.51cto.com/12318731/1894285