原文来自:http://stackoverflow.com/questions/419163/what-does-if-name-main-do
http://ibiblio.org/g2swap/byteofpython/read/module-name.html
How It Works
Every Python module has it's __name__
defined and if this is '__main__'
, it implies that the module is being run standalone by the user and we can do corresponding appropriate actions.
总结下,就是看 是不是自己模块直接运行(如果是,就运行if下面部分的代码),还是被加载进去运行(就不运行if的代码了)。