您的位置:首页 > 编程语言 > Python开发

python:if __name__== "__main__" 的意思(作用)

2017-06-08 14:59 711 查看
if _name_== “_main_” 的意思(作用)python代码复用

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

if __name__== "__main__":
main()


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

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

#!/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!!还有一个好的方法就是可以去将上面的代码功能封装成模块,然后通过模块调用其中的函数,方便我们多次调用,而又不需要重新改写代码。下面是将上面的代码封装好,函数化,放到模块中的改进脚本:

#!/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有两种使用方式,一种是作为模块被调用,另外一种是直接运行。那么如何判断呢?于是就有了以下改进!!!

#!/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()函数不会被执行。

模块是对象,并且所有的模块都有一个内置属性 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文件被使用的方式。从而方便我们代码复用,也可以测试模块。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: