DocStringsDocStrings are an important tool for explaining and documenting programs, helping make your program documentation simpler and easier to understand.
We can use a pair of triple single quotes on the first line of the function body'''or a pair of triple double quotes"""to define a docstring.
You can use__doc__(note the double underscores) to access the function's docstring attribute.
#!/usr/bin/python
# -*- coding: UTF-8 -*-
def function():
''' say something here!
'''
pass
print (function.__doc__) # Call doc
The output is:
say something here!
DocStringsDocstring convention: the first line briefly describes the function's purpose, the second line is blank, and the third line contains a detailed description.
#!/usr/bin/python
# -*- coding: UTF-8 -*-
def printMax(x,y):
'''Print the maximum of two numbers.
Both values must be integers.'''
x=int(x)
y=int(y)
if x>y:
print(x,'Maximum')
else:
print(y,'Maximum')
printMax(3,5)
print (printMax.__doc__) # Call doc
The output is:
5 最大
打印两个数中的最大值。
两个值必须都是在整形数。