python中 __name__及__main()__的妙处
来源:互联网 发布:伊利丹7.3知乎 编辑:程序博客网 时间:2024/06/04 18:31
本文转载自Leo Chin
http://www.cnblogs.com/hnrainll/archive/2011/09/01/2161943.html
举例说明
#hello.pydef sayHello(): str="hello" print(str);if __name__ == "__main__": print ('This is main of module "hello.py"') sayHello()
1、当单独执行该module时,比如单独执行以上hello.py: python hello.py,则输出
This is main of module "hello.py"hello
可以理解为”if __name__==”__main__“:” 这一句与c中的main()函数所表述的是一致的,即作为入口;
2、当该module被其它module 引入使用时,其中的”if __name__==”__main__“:”所表示的Block不会被执行,这是因为此时module被其它module引用时,其__name__的 值将发生变化,__name__的值将会是module的名字。比如在python shell中import hello后,查看hello.__name__:
>>> import hello>>> hello.__name__'hello'>>>
3、因此,在python中,当一个module作为整体被执行时,module.__name__的值将是”__main__“;而当一个 module被其它module引用时,module.__name__将是module自己的名字,当然一个module被其它module引用时,其 本身并不需要一个可执行的入口main了。
转自:http://www.cnblogs.com/liqilei/archive/2010/08/11/1797715.html
阅读全文
0 0
- python中 __name__及__main()__的妙处
- python中 __name__及__main()__的妙处
- python中 __name__及__main()__的妙处
- python中 __name__及__main()__的妙处
- python中 __name__及__main()__的妙处
- python中 __name__及__main()__的妙处
- python中 __name__及__main()__的妙处
- python中 __name__及__main()__的妙处
- python中 __name__及__main()__
- python中 __name__及__main()__的使用
- python中 __name__及__main()__的使用
- python中 __name__及__main()__的使用
- if __name__=='__main()__:
- python中 __name__ 的解析
- python中__name__的解析
- python中__name__的使用
- 【整理】python 中 self, init, __ ,if __name__ == '__main__'
- python中出现的__name__的原因
- Genymotion安装
- JSON数据解析及gson.jar包
- TensorFlow命名空间和TensorBoard图节点
- 水仙花的数量
- Python中string总结
- python中 __name__及__main()__的妙处
- Android 6.0 7.0 8.0特性变更
- 针对Android的木马程序Loapi好毒!可以真正的破坏整支手机
- SpringMVC
- 视频帧数的控制
- 读阿里巴巴开发手册总结
- 畸变矫正(反畸变)
- PowerDesigner 怎么将表生成Html Report
- 守护进程c实现