Python学习日志(二)

来源:互联网 发布:以撒的结合mac下载 编辑:程序博客网 时间:2024/06/06 12:50

函数中的DocStrings
Python有一个很奇妙的特性,称为 文档字符串 ,它通常被简称为 docstrings 。DocStrings是一个重要的工具,由于它帮助你的程序文档更加简单易懂,你应该尽量使用它。你甚至可以在程序运行的时候,从函数恢复文档字符串!
例:

#!/usr/bin/python# Filename: func_doc.pydef printMax(x, y):    '''Prints the maximum of two numbers.    The two values must be integers.'''    x = int(x) # convert to integers, if possible    y = int(y)    if x > y:        print x, 'is maximum'    else:        print y, 'is maximum'printMax(3, 5)print printMax.__doc__

输出:

$ python func_doc.py5 is maximumPrints the maximum of two numbers.        The two values must be integers.

文档字符串的惯例是一个多行字符串,它的首行以大写字母开始,句号结尾。第二行是空行,从第三行开始是详细的描述。
你可以使用_doc_(注意双下划线)调用printMax函数的文档字符串属性(属于函数的名称)。请记住Python把 每一样东西 都作为对象,包括这个函数。