python中 __name__及__main()__的妙处

#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

可以理解为"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作为整体被执行时,moduel.__name__的值将是"__main__";而当一个 module被其它module引用时,module.__name__将是module自己的名字,当然一个module被其它module引用时,其 本身并不需要一个可执行的入口main了。可以说python中的这种用法很灵活啊。

转自:http://www.cnblogs.com/liqilei/archive/2010/08/11/1797715.html

python中 __name__及__main()__的妙处的更多相关文章

  1. python中 __name__及__main()__的使用

    python中 __name__及__main()__的使用 #hello.py def sayHello(): str="hello" print(str); if __name ...

  2. python __name__及__main()__的妙处

    #hello.py def sayHello(): str="hello" print(str); if __name__ == "__main__": pri ...

  3. 浅析python 中__name__ = '__main__' 的作用

    引用http://www.jb51.net/article/51892.htm 很多新手刚开始学习python的时候经常会看到python 中__name__ = \'__main__\' 这样的代码 ...

  4. 【转】浅析python 中__name__ = '__main__' 的作用

    原文链接:http://www.jb51.net/article/51892.htm 举例说明解释的非常清楚,应该是看到的类似博文里面最简单的一篇: 这篇文章主要介绍了python 中__name__ ...

  5. 002_浅析python 中__name__ = '__main__' 的作用

    很多新手刚开始学习python的时候经常会看到python 中__name__ = \'__main__\' 这样的代码,可能很多新手一开始学习的时候都比较疑惑,python 中__name__ = ...

  6. 理解 python 中__name__ = '__main__' 的作用

    很多新手刚开始学习python的时候经常会看到python 中__name__ = \'__main__\' 这样的代码,可能很多新手一开始学习的时候都比较疑惑,python 中__name__ = ...

  7. python 中__name__ = '__main__' 的作用,到底干嘛的?

    python 中__name__ = 'main' 的作用,到底干嘛的? 有句话经典的概括了这段代码的意义: "Make a script both importable and execu ...

  8. python学习笔记26(python中__name__的使用)

    在python中,每个py文件都是一个模块,也都是一个可执行文件,即包含main方法.因此,对每个py文件,可以单独运行,也可以import它给其他客户使用,这两种情况不一样. 1. 如果模块是被导入 ...

  9. Python中__name__属性的妙用

    在Python中,每一个module文件都有一个built-in属性:__name__,这个__name__有如下特点: 1 如果这个module文件是被别的文件导入的,那么,该__name__属性的 ...

随机推荐

  1. Python3网络爬虫(3):使用User Agent和代理IP隐藏身份

    Python版本: python3 IDE: pycharm2017.3.3 一.为何要设置User Agent 有一些网站不喜欢被爬虫访问,所以会检测对象,如果是爬虫程序,他就会不让你访问,通过设置 ...

  2. 向git添加和提交文件

    状态 git status 可以知道有哪些文件被修改,哪些文件待提交 当前无待提交文件 分区 三个分区:工作区,缓存区,版本库 三个分区之间的联系: 工作区 >> git add > ...

  3. MT【230】一道代数不等式

    设$a,b,c>0,$满足$a+b+c\le abc$证明:$\dfrac{1}{\sqrt{1+a^2}}+\dfrac{1}{\sqrt{1+b^2}}+\dfrac{1}{\sqrt{1+ ...

  4. 自学Python6.2-类、模块、包

    自学Python之路-Python基础+模块+面向对象自学Python之路-Python网络编程自学Python之路-Python并发编程+数据库+前端自学Python之路-django 自学Pyth ...

  5. 【51NOD 1847】奇怪的数学题(莫比乌斯反演,杜教筛,min_25筛,第二类斯特林数)

    [51NOD 1847]奇怪的数学题(莫比乌斯反演,杜教筛,min_25筛,第二类斯特林数) 题面 51NOD \[\sum_{i=1}^n\sum_{j=1}^nsgcd(i,j)^k\] 其中\( ...

  6. hive1.1.0安装

    0. 说明 已经安装好Hadoop-1.2.1. 安装好mysql. 1.解压安装包 先把安装包下载.然后解压: tar -zxvf apache-hive-1.1.0-bin.tar.gz 2.配置 ...

  7. Spring中的ApplicationContextAware使用

          加载Spring配置文件时,如果Spring配置文件中所定义的Bean类实现了ApplicationContextAware 接口,那么在加载Spring配置文件时,会自动调用Applic ...

  8. js数组作为参数用ajax向后台传参数

    /*前台往后台传参数时,可以这样写*/ var chessId = "123"; var i=0; var data = []; /*添加单个参数*/ data.push({nam ...

  9. vs widows服务的调试

    1.使用.net 工具安装你开发好的服务 2.服务运行后在Vs中选择调试>附加到进程 4.选择安装好运行的服务,选择附加

  10. springboot的小知识总结

    1.RestTemplate提交表单数据的三种方法 https://blog.csdn.net/yiifaa/article/details/77939282 2.spring data的分页实现:p ...