跟老齐学Python之Python文档(2)
有一种方法可以实现,就是在你所编写的程序中用三个双引号或者单引号成对地出现,中间写上有关文档内容。
>>> def qiwsir():
... """I like python"""
... print "http://qiwsir.github.io"
...
>>> qiwsir()
http://qiwsir.github.io
>>> print(qiwsir.__doc__) #用这种方法可以看自己写的函数中的文档
I like python
>>> help(qiwsir) #其实就是调用__doc__显示的内容
Help on function qiwsir in module __main__:
qiwsir()
I like python
另外,对于一个文件,可以把有关说明放在文件的前面,不影响该文件代码运行。
例如,有这样一个扩展名是.py的python文件,其内容是:
#!/usr/bin/env python
#coding:utf-8
import random
number = random.randint(1,100)
guess = 0
while True:
num_input = raw_input("please input one integer that is in 1 to 100:")
guess +=1
if not num_input.isdigit():
print "Please input interger."
elif int(num_input)<0 and int(num_input)>=100:
print "The number should be in 1 to 100."
else:
if number==int(num_input):
print "OK, you are good.It is only %d, then you successed."%guess
break
elif number>int(num_input):
print "your number is more less."
elif number<int(num_input):
print "your number is bigger."
else:
print "There is something bad, I will not work"
这段程序,就是在《用while来循环》中用到的一个猜数字的游戏,它存储在名为205-2.py的文件中,如果要对这段程序写一个文档,就可以这么做。
"""
This is a game.
I am Qiwei.
I like python.
I am writing python articles in my website.
My website is http://qiwsir.github.io
You can learn python free in it.
"""
#!/usr/bin/env python
#coding:utf-8
import random
number = random.randint(1,100)
guess = 0
while True:
num_input = raw_input("please input one integer that is in 1 to 100:")
guess +=1
if not num_input.isdigit():
print "Please input interger."
elif int(num_input)<0 and int(num_input)>=100:
print "The number should be in 1 to 100."
else:
if number==int(num_input):
print "OK, you are good.It is only %d, then you successed."%guess
break
elif number>int(num_input):
print "your number is more less."
elif number<int(num_input):
print "your number is bigger."
else:
print "There is something bad, I will not work"
最后,推荐一片相当相当好的文章,与列位分享:
Python 自省指南:如何监视您的 Python 对象
- 上一篇:跟老齐学Python之重回函数
- 下一篇:跟老齐学Python之大话题小函数(2)