if __name__== "__main__" 的意思(作用)python代码复用

时间:2021-12-15 01:42:22

if __name__== "__main__" 的意思(作用)python代码复用

转自:大步's Blog   http://www.dabu.info/if-__-name__-__main__-mean-function-python-code-reuse.html

有人在学习python脚本时会发现有的脚本下面有几行代码;

1
2
if __name__== "__main__":
main()

不明白其中的意思,其实这就是方便我们代码复用的,我们可以在其他脚本方便的调用另外一个脚本里面的函数。

假设下载我有下面一个python代码,叫做: pysysinfo.py


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/usr/bin/env python
#A system information gathering script
import subprocess
#command 1          //这段代码是查询系统信息的
uname = "uname"        
uname_arg = "-a"
print "gathering system information with $s command:\n" % uname
subprocess.call([uname,uname_arg])
#command 2       //这段代码是查询磁盘分区使用情况的
diskspace = "df"
diskspace_arg = "-h"
print "gathering diskspace information %s command:\n"  %  diskspace
subprocess.call([diskspace,diskspace_arg])

但是如果我们只想用其中第一段command 1的代码查询系统信息,怎么办呢?难道把这个脚本的后面command 2代码删除?that is foolish!!还有一个好的方法就是可以去将上面的代码功能封装成模块,然后通过模块调用其中的函数,方便我们多次调用,而又不需要重新改写代码。下面是将上面的代码封装好,函数化,放到模块中的改进脚本:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/usr/bin/env python
#A system information gathering script
import subprocess
#command 1
def uname_func()
uname = "uname"
uname_arg = "-a"
print "gathering system information with $s command:\n" % uname
subprocess.call([uname,uname_arg])
#command 2
def disk_func()
diskspace = "df"
diskspace_arg = "-h"
print "gathering diskspace information %s command:\n"  %  diskspace
subprocess.call([diskspace,diskspace_arg])
#main function that call other functions
def main()
uname_fun()
diskspace_fun()
main()

问题是当我们要调用其中的函数的时候,如何让脚本知道是我们到底是想让它直接运行还是只是作为模块引入另外一个脚本呢?.py有两种使用方式,一种是作为模块被调用,另外一种是直接运行。那么如何判断呢?于是就有了以下改进!!!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#!/usr/bin/env python
#A system information gathering script
import subprocess
#command 1
def uname_func()
uname = "uname"
uname_arg = "-a"
print "gathering system information with $s command:\n" % uname
subprocess.call([uname,uname_arg])
#command 2
def disk_func()
diskspace = "df"
diskspace_arg = "-h"
print "gathering diskspace information %s command:\n"  %  diskspace
subprocess.call([diskspace,diskspace_arg])
#main function that call other functions
def main()
uname_fun()
diskspace_fun()
if __name__== "__main__":
main()

在main()函数前加入了一个判断语句,以此来让脚本判断自己是被当做模块调用,还是被直接运行的。当被import作为模块调用的时候,if以下的代码就不会被执行,也就是说main()函数不会被执行。

为了更好说明,引用下面一篇文章来让我们更加明白

python中if __name__ == '__main__': 的解析

模块是对象,并且所有的模块都有一个内置属性 __name__。一个模块的 __name__ 的值取决于您如何应用模块。如果 import 一个模块,那么模块__name__ 的值通常为模块文件名,不带路径或者文件扩展名。但是您也可以像一个标准的程序样直接运行模块,在这 种情况下, __name__ 的值将是一个特别缺省"__main__"。

///////////////////////////////////////////////////////////////////////////////////////////////////

在cmd 中直接运行.py文件,则__name__的值是'__main__';

而在import 一个.py文件后,__name__的值就不是'__main__'了;

从而用if __name__ == '__main__'来判断是否是在直接运行该.py文件

如:

#Test.py

class Test:

def __init(self):pass

def f(self):print 'Hello, World!'

if __name__ == '__main__':

Test().f()

#End

你在cmd中输入:

C:>python Test.py

Hello, World!

说明:"__name__ == '__main__'"是成立的

你再在cmd中输入:

C:>python

>>>import Test

>>>Test.__name__                #Test模块的__name__

'Test'

>>>__name__                       #当前程序的__name__

'__main__'

无论怎样,Test.py中的"__name__ == '__main__'"都不会成立的!

所以,下一行代码永远不会运行到!

通过这篇文章,我们知道,.py文件会根据自身的使用方式,它的__name__的属性会不同。如果他的属性是“__main__”,那么脚本就知道自己是被直接执行的。如果是被别的文件import的来用,__name__的属性就会是脚本自己的文件名,从而防止错误的执行。引入的if语句的作用就是  通过检查该模块的__name__属性来实现判断.py文件被使用的方式。从而方便我们代码复用,也可以测试模块。