1. 如果模块是被导入,__name__的值为模块名字
2. 如果模块是被直接执行,__name__的值为'__main__'
Py1.py
代码如下:
#!/usr/bin/env python
def test():
print '__name__ = ',__name__
if __name__ == '__main__':
test()
代码如下:
#!/usr/bin/env python
import Py1.py
def test():
print '__name__ = ',__name__
if __name__ == '__main__':
test()
print ‘Py1.py __name__ = ',Py1.__name__
代码如下:
__name__=__main__
Py1.py __name__=Py1
代码如下:
if __name__ == '__main__':
test()