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. N皇后问题(DFS)

    题目:在N*N的国际象棋棋盘上放置N个皇后彼此不受攻击(即在棋盘的任一行,任一列和任意对角线上不能放置2个皇后),求解所有摆放方案的总数. 样例输入: 1 8 样例输出: 1 92 解题思路:由于皇后 ...

  2. 洛谷P4043 支线剧情

    题意:给定DAG,通过每条边需要时间. 从某号点回到1号点不需要时间. 从1号点出发,求最少要多久才能走完所有边. 解: 有源汇有上下界最小费用可行流. 直接连边,费用为时间,下界为1,无上界. 每个 ...

  3. A1058. A+B in Hogwarts

    If you are a fan of Harry Potter, you would know the world of magic has its own currency system -- a ...

  4. NO.8:绝不在构造或者析构过程中调用virtual函数

    在构造和析构执行期间不要调用virtual函数,因为这类调用从不会下降至derived class(比起当前执行构造函数和析构函数) 如果在base class 构造函数或者析构函数调用virtual ...

  5. 【leetcode】 Merge Intervals

    Merge Intervals Given a collection of intervals, merge all overlapping intervals. For example,Given  ...

  6. 【Maven】eclipse中使用Maven、生命周期

    1.在eclipse中创建maven工程 >>在eclipse中配置maven: 配置maven版本:Eclips自带了一个maven,一般不用自带的这个,而选择我们安装的那个maven ...

  7. Linux中如何安装RAR

    在Windows下的winrar几乎一统压缩软件的市场占有率,winrar只是RAR在Windows环境下的图形界面而已,核心功能还是RAR,那么如何在Linux中安装RAR呢? 1.下载RAR下载地 ...

  8. 解决编译安装php时报错:Please reinstall the iconv library

    编译安装php7时报错“Please reinstall the iconv library”,也就是让重新安装iconv库.但yum安装又提示“No package libiconv availab ...

  9. 面向对象【day07】:类的特性介绍(四)

    本节内容 1.概述 2.访问属性 3.私有属性 4.总结 一.概述 在上篇博客中我们已经讲了一些关于类的知识,我们来回顾以下: 定义类(class dog(object))-> 实例化(d = ...

  10. Centos7系统初始化脚本

    前言: 因公司业务增加,陆续新增服务器,时不时的来几台,手动地一台台对服务器初始化操作感觉太麻烦. 于是乎,根据初始化需求整合了一个初始化脚本,实现批量脚本初始化操作. 说明: 本脚本根据自身需求编写 ...