python中 __name__及__main()__的使用
来源:互联网 发布:linux下安装jdk 编辑:程序博客网 时间:2024/06/11 22:37
原文地址:http://www.cnblogs.com/liqilei/archive/2010/08/11/1797715.html
#hello.py
def sayHello():
str="hello"
print(str);
if __name__ == "__main__":
print ('This is main of module "hello.py"')
sayHello()
python作为一种脚本语言,我们用python写的各个module都可以包含以上那么一个累死c中的main函数,只不过python中的这种__main__与c中有一些区别,主要体现在:
1、当单独执行该module时,比如单独执行以上hello.py: python hello.py,则输出
This is main of module "hello.py"
hello
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'
>>>
>>> hello.__name__
'hello'
>>>
3、因此,在python中,当一个module作为整体被执行时,moduel.__name__的值将是"__main__";而当一个 module被其它module引用时,module.__name__将是module自己的名字,当然一个module被其它module引用时,其 本身并不需要一个可执行的入口main了。可以说python中的这种用法很灵活啊。
阅读全文
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学习笔记26(python中__name__的使用)
- python中 __name__ 的解析
- python中__name__的解析
- python中的__name__的使用
- java synchronized用法总结
- 使input文本框不可编辑的3种方法
- 导项目
- MFC中的一些数据转换
- Excel插入当前日期
- python中 __name__及__main()__的使用
- Android SIM卡联系人操作总结
- SCRIPT70: 没有权限
- thinkphp5实战系列(一)前期配置
- Git使用
- 表单验证+实现跳转
- js grid renderer用法
- 禁止网站被另存为的几种方法
- 单/多图像显示