转载:https://stackoverflow.com/questions/419163/what-does-if-name-main-do When your script is run by passing it as a command to the Python interpreter, python myscript.py all of the code that is at indentation level 0 gets executed. Functions and class…
最近刚刚学习python,看到别人的源代码中经常出现这样一个代码段: if __name__ = '__main__' dosomting() 觉得很晕,不知道这段代码的作用是什么,后来上网查了一些资料,有个老外用一句话概括了这段代码的意义: ”Make a script both importable and executable“ 意思就是说让你写的脚本模块既可以导入到别的模块中用,另外该模块自己也可执行. 这样解释对于新手来说可能还有些迷糊,下面就举个栗子来说明一下吧^_^: #modul…