if __name__ == '__main__':
- make a script both importable and executable
- Don't BiuBiu, show me your code :
# test.py
def main():
print "we are in %s",%__name__
if __name__ == '__main__':
main()
直接执行上述代码,将在屏幕上打印we are in main 说明if语句内容被执行,调用了main();
倘若我们在另一个模块中导入该模块时,其执行结果为:we are in test
-- 如果我们是直接执行某个.py文件,该文件中那么"__name__"= __main__