python __name__=='__main__'用法
2013-12-25 14:32
218 查看
当你打开一个.py文件时,经常会在代码的最下面看到if __name__ == '__main__':,现在就来介 绍一下它的作用.
模块是对象,并且所有的模块都有一个内置属性 __name__。一个模块的 __name__ 的值取决于您如何应用模块。如果 import 一个模块,那么模块__name__ 的值通常为模块文件名,不带路径或者文件扩展名。但是您也可以像一个标准的程序样直接运行模块,在这 种情况下, __name__ 的值将是一个特别缺省"__main__"。
///////////////////////////////////////////////////////////////////////////////////////////////////
在cmd 中直接运行.py文件,则__name__的值是'__main__';
而在import 一个.py文件后,__name__的值就不是'__main__'了;
从而用if __name__ == '__main__'来判断是否是在直接运行该.py文件
如:
#Test.py
class Test:
def __init(self):pass
def f(self):print 'Hello, World!'
if __name__ == '__main__':
Test().f()
#End
你在cmd中输入:
C:>python Test.py
Hello, World!
说明:"__name__ == '__main__'"是成立的
你再在cmd中输入:
C:>python
>>>import Test
>>>Test.__name__ #Test模块的__name__
'Test'
>>>__name__ #当前程序的__name__
'__main__'
无论怎样,Test.py中的"__name__ == '__main__'"都不会成立的!
所以,下一行代码永远不会运行到!
When you run a Python module with
the code in the module will be executed, just as if you imported it, but with the __name__ set to "__main__".
That means that by adding this code at the end of your module:
you can make the file usable as a script as well as an importable module, because the code that parses the command line only runs if the module is executed as the “main” file:
If the module is imported, the code is not run:
>>>
模块是对象,并且所有的模块都有一个内置属性 __name__。一个模块的 __name__ 的值取决于您如何应用模块。如果 import 一个模块,那么模块__name__ 的值通常为模块文件名,不带路径或者文件扩展名。但是您也可以像一个标准的程序样直接运行模块,在这 种情况下, __name__ 的值将是一个特别缺省"__main__"。
///////////////////////////////////////////////////////////////////////////////////////////////////
在cmd 中直接运行.py文件,则__name__的值是'__main__';
而在import 一个.py文件后,__name__的值就不是'__main__'了;
从而用if __name__ == '__main__'来判断是否是在直接运行该.py文件
如:
#Test.py
class Test:
def __init(self):pass
def f(self):print 'Hello, World!'
if __name__ == '__main__':
Test().f()
#End
你在cmd中输入:
C:>python Test.py
Hello, World!
说明:"__name__ == '__main__'"是成立的
你再在cmd中输入:
C:>python
>>>import Test
>>>Test.__name__ #Test模块的__name__
'Test'
>>>__name__ #当前程序的__name__
'__main__'
无论怎样,Test.py中的"__name__ == '__main__'"都不会成立的!
所以,下一行代码永远不会运行到!
When you run a Python module with
python fibo.py <arguments>
the code in the module will be executed, just as if you imported it, but with the __name__ set to "__main__".
That means that by adding this code at the end of your module:
if __name__ == "__main__": import sys fib(int(sys.argv[1]))
you can make the file usable as a script as well as an importable module, because the code that parses the command line only runs if the module is executed as the “main” file:
$ python fibo.py 50 1 1 2 3 5 8 13 21 34
If the module is imported, the code is not run:
>>>
>>> import fibo >>>
相关文章推荐
- 【整理】python 中 self, init, __ ,if __name__ == '__main__'
- python中if __name__ == '__main__': 的解析
- python中if __name__ == '__main__': 的解析
- python文件中 `if __name__ == '__main__':` 的作用
- 浅析python 中__name__ = '__main__' 的作用
- python中if __name__ == '__main__': 的解析
- python中if __name__ == '__main__': 的解析
- python中if __name__ == '__main__': 的解析
- python中if __name__ == '__main__': 的解析
- python中if __name__ == '__main__': 的解析
- python中if __name__ == '__main__': 的解析
- python中if __name__ == '__main__': 的解析
- python中if __name__ == '__main__': 的解析
- python中if __name__ == "__main__":用法解析
- 浅析python 中__name__ = '__main__' 的作用
- 【Python学习笔记】关于if __name__ == '__main__'
- python中if __name__ == '__main__': 的解析
- python文件中 if __name__ == '__main__': 的作用
- python 中的if __name__ == 'main':
- 浅析python 中__name__ = '__main__' 的作用