Python有一个很奇妙的特性,称为 文档字符串 ,它通常被简称为 docstrings 。
程序 func_doc.py
-------------------------------------------------------------------------------------------------
#!/usr/bin/python
# Filename: func_doc.py
def 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__
-------------------------------------------------------------------------------------------------
在函数的第一个逻辑行的字符串是这个函数的 文档字符串 。注意,DocStrings也适用于模块和类。
文档字符串的惯例是一个多行字符串,它的首行以大写字母开始,句号结尾。第二行是空行,从第三行开始是详细的描述。
你可以使用__doc__(注意双下划线)调用printMax函数的文档字符串属性(属于函数的名称)。
help()所做的只是抓取函数的__doc__属性,然后整洁地展示给你。