if __name__ == '__main__'

2016-04-17  本文已影响739人  Luculent

在python的代码中经常会看到这样的情况:

def f():
  print 'hello world!'
def main():
  f()
if __name__ == '__main__':
  main()

一开始编程的时候觉得特别别扭,我为什么不可以直接写成:

def f():
  print 'hello world!'
f()

所以我就去查第一种写法的好处了。幸亏有我这种困惑的人还蛮多的,很快就查到了有关资料。

Python 下划线和有关规范

首先得明白为啥有__init__,__name__,__main__这种变量存在。随便一查,就看到了<Python 下划线和有关规范>这篇文章。之前以为前后两个下划线是配套使用的,其实并不完全是。

也就是说除了前后双下划线以外,其它的只是一种命名规则,如果你在变量名前面加单下划线/双下划线,就一定程度地变成"局部变量",当别人调用你的整块代码时,不容易被发现这个变量。例如说我有一个数单词的代码count.py,里面除了函数num可以返回某个单词的字数外,还有一个函数search用来查找单词,但是如果我用了上面的规则来定义这个查找单词的函数返回的变量,别人import count.py后可以使用count.num,但不能简单地调用count.search

条件句if __name__ == '__main__'

根据<浅析python 中__name__ == '__main__'的作用>中提到的

“Make a script both importable and executable”
意思就是说让你写的脚本模块既可以导入到别的模块中用,另外该模块自己也可执行

整篇博客看下来,再查找python的document,可以简单地解释如下:
__name__是python一个内置变量,用来表示调用方式。当直接运行count.py时,调用方式就是__main__,也就是count.py作为脚本,这时if __name__ == '__main__'的值当然为真。当count.py作为模块在别的代码,比如在import_count.py中调用时,那时的__name__的值是是import_count.py'__main__',也就是import_count,那么条件句不成立,下面的代码就不会被执行。
在被其他代码调用的时候条件句不会被执行,那么我们就可以在下面乱来了。例如写测试代码:

# match_ends.py
# Given a list of strings, return the count of the number of
# strings where the string length is 2 or more and the first
# and last chars of the string are the same.
def match_ends(words): 
  count = 0  
  for word in words:
    if len(word) >= 2 and word[0] == word[-1]: 
       count = count + 1  
       return count 
def test(got, expected):
  if got == expexted: prefix = 'OK'
  else: prefix = 'X'
  print '%s got: %s expected: %s' % (prefix, repr(got), repr(expected))
def main():
  print 'match_ends'
  test(match_ends(['aba', 'xyz', 'aa', 'x', 'bbb']), 3)
if __name__ == '__main__':
  main()

这样的话,我在运行match_ends.py时,就可以测试代码是对是错,在调用match_ends.py时,就不会显示

>>>match_ends
   OK  got: 3 expected: 3

这种测试的直观结果了。

上一篇 下一篇

猜你喜欢

热点阅读