pytion3--文档字符串:__doc__
除了#注释外,Python也支持可自动附加在对象上的文档,而且在运行时还可保存查看。从语法上来说,这类注释是写成字符串,放在模块文档、函数以及类语句的顶端。就在任何可执行程序代码前(#注释在其前也没问题)。Python会自动封装这个字符串,也就是成为所谓的文档字符串,使其成为相应对象的__doc__属性。
用户定义的文档字符串
例如,考虑下面的文件docstrings.py。其文档字符串出现在文件开端以及其中的函数和类的开头。在这里,文件和函数多行注释使用的是三重引号块字符串,但是任何类型的字符串都能用。
"""
Module documentation
Words Go Here
"""
#!/usr/bin/env python
# -*- coding:utf-8 -*-
spam = 40
def square(x):
"""
fouction documetation
can we have your liver hten?
:param x:
:return:
"""
return x **2
class emplouee:
"class documetation"
pass
print(square(4))
print(square.__doc__)
运行结果:
16
fouction documetation
can we have your liver hten?
:param x:
:return:
考虑下面的文件docstrings.py
""" Module documentation Words Go Here """ #!/usr/bin/env python # -*- coding:utf-8 -*- import docstrings print(docstrings.__doc__) print(docstrings.square.__doc__) print(docstrings.emplouee.__doc__)
运行结果:
Module documentation
Words Go Here
fouction documetation
can we have your liver hten?
:param x:
:return:
class documetation
浙公网安备 33010602011771号