Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4427965
  • 博文数量: 252
  • 博客积分: 5347
  • 博客等级: 大校
  • 技术积分: 13838
  • 用 户 组: 普通用户
  • 注册时间: 2009-09-30 10:13
文章分类
文章存档

2022年(12)

2017年(11)

2016年(7)

2015年(14)

2014年(20)

2012年(9)

2011年(20)

2010年(153)

2009年(6)

分类: Python/Ruby

2014-05-14 20:50:23

      当打开一个Python文件时,通常是.py作为扩展名,我们通常会在代码的最后面看到If __name__ == “__main__”:这条语句,这条语句的主要作用就是当该文件直接被使用时,就会__name__就等于__main__,当作为模块被调用时,__name__就不等于__main__了,这样我们就可以在模块中添加自己的打印调试信息。首先,我们可以看下面的的一个例子。

例子1:我们定义了一个自己的简单模块,这个模块的名字是my_square.py,就是计算一个数的平方。

点击(此处)折叠或打开

  1. #!/usr/bin/python
  2. def square(x):
  3.     y = x * x
  4.     return y
  5. if __name__ == "__main__":
  6.     print "my math module square(5)=",square(5);

    该文件直接运行时会打印输出,但是当使用import导入到另一个模块时,该语句并不会输出

     我们直接运行该程序,可以查看该运行结果。

点击(此处)折叠或打开

  1. root@team:~/python# my_square.py
  2. my math module square(5)= 25

    直接运行该模块可以看到if条件的判断是成立的,也就是这里name__main__,可以添加自己的调试信息。我们再写一个简单的程序,在该程序中使用import导入我们刚才写的square模块。程序的名字为test_name_main.py

点击(此处)折叠或打开

  1. root@team:~/python# cat test_name_main.py
  2. #!/usr/bin/python
  3. import my_square;
  4. print "In caller,Call square from my_square ,square(8)",my_square.square(8);
  5. print "my_square module be imported,my_square module name =",my_square.__name__;
  6. print "module name =",__name__;
    我们查看一下运行结果:


点击(此处)折叠或打开

  1. root@team:~/python# ./test_name_main.py
  2. In caller,Call square from my_square ,square(8) 64
  3. my_square module be imported,my_square module name = my_square
  4. module name = __main__

通过运行结果我们可以看到在my_square.py文件中的if判断条件不成立,所以         print "my math module square(5)=",square(5);并没有打印出来,我们使用my_square获取其__name__属性,输出的结果为my_square

         因此,我们可以使用该特性在我们自己的模块中添加一下调试信息,这些调试信息仅仅当直接运行是才会打印出来,但作为模块导入到另一个文件时,并不会被打印出来。

         Python2.7.2 documentation文件中我们可以看到对__name__的解释。__name__ is to be set to the name of the module

         Python2.7.2 documentation文档中The Python Language Reference章节中Data model中也有对__name__的介绍:

__name__ is the method name (same as im_func.__name__);

__name__ is the function’s name

Predefined (writable) attributes: __name__ is the module’s name

Special attributes: __name__ is the class name

         总之,通过上面的官方解释可知,__name__在不同的语义环境中可以表示不同的名字。如果在Class中就是类的名字,如果在函数中就是函数的名字,在模块中就是模块名字。在Python2.7.2 documentation文档中,有对__main__的解释

The namespace for a module is automatically created the first time a module is imported. The main module for a script is always called __main__.

         对于Python程序语言来说,我们更多情况下是使用模块的概念,也就是在写Python程序中使用import 导入一个模块,在Python的库中有许多写好的模块可以直接使用。所以,当一个模块被第一次导入时,就会自动创建一个命名空间,此时脚本的主模块的名字始终是__main__。其实对于Python程序分为直接执行和作为模块调用执行,例如上面的my_square.py就是作为模块调用执行的,在test_name_main.py

         通过上面的示例程序可以看到:

(1)       my_square.py作为自身直接运行时,__name__对应的就是__main__

(2)       my_square.py作为一个模块被别人调用时,对应的模块my_square.py的模块名字为my_square.

示例二:

         在网上有一个经典的例子可以参考,


点击(此处)折叠或打开

  1. Example? 8.2.? Using a module's __name__
  2.                 
  3. #!/usr/bin/python
  4. # Filename: using_name.py

  5. if __name__ == '__main__':
  6.     print 'This program is being run by itself'
  7. else:
  8.     print 'I am being imported from another module
     也可以说明上面的问题。
     总之,__name__表示的是模块或者函数或者类的名字。如果是.py自身执行时,__name__就等于__main__,如果是作为模块导入到.py文件后,模块的名字就是模块本身的名字。
参考文献: 
阅读(6359) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~