Python关于 if __name__ == "__main__"

Following words copyed from Python documentation

__name__

The __name__ attribute must be set to the fully-qualified name of the module. This name is used to uniquely identify the module in the import system.

__main__

__main__ is the name of the scope in which top-level code executes. A module’s __name__ is set equal to '__main__' when read from standard input, a script, or from an interactive prompt.

A module can discover whether or not it is running in the main scope by checking its own __name__, which allows a common idiom for conditionally executing code in a module when it is run as a script or with python -m but not when it is imported:

if __name__ == "__main__":
# execute only if run as a script
main()

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,453评论 0 10
  • 今天,我把花盆挖了个洞,再把苹果种子种在花盆里,并埋住。 每...
    李雅康阅读 375评论 0 0
  • 看完这些也就算是可以玩开发了 1os是管理和控制计算机软件和硬件资源的计算机程序;是直接运行在裸机上的系统软件;任...
    资深程序猿阅读 435评论 0 0
  • 夏天是一个非常适合撸串的季节,从南到北,从东到西,不同的地方都有自己当地的特色夜宵,撸串只是夜宵的一种,但是深得男...
    韵月思密达阅读 516评论 2 1