在我们写一些脚本的时候一定要加上注释,不然时间久了可能自己都记不清楚当时这些代码是做什么用的了,在python中,我们也会对一些函数做一些描述,但是有时候不打开代码是看不到每个函数的解释的,python提供了一个很方便的工具docstrings(documentation strings)文档字符,可以很方便的打印出函数的描述,我自己写了一个小例子。
#-*-coding:utf-8-*- #author:yangdong def print_documentation(x,y): '''this is a function to learn how to make __doc__ to print the documentation!! the function's result is the max number''' x=int(x) y=int(y) if x > y: return x elif x == y: return "the tow number is equal!" else: return y print(print_documentation(4,6)) print(print_documentation.__doc__)
运行结果:
6 this is a function to learn how to make __doc__ to print the documentation!! the function's result is the max number