龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > python编程 >

跟老齐学Python之Python文档(2)

时间:2014-10-11 02:50来源:网络整理 作者:网络 点击:
分享到:
有一种方法可以实现,就是在你所编写的程序中用三个双引号或者单引号成对地出现,中间写上有关文档内容。 复制代码 代码如下: def qiwsir(): ... """I li

有一种方法可以实现,就是在你所编写的程序中用三个双引号或者单引号成对地出现,中间写上有关文档内容。

复制代码 代码如下:

>>> 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 对象

精彩图集

赞助商链接