转:python中的文档字符串(docStri…
来源:互联网 发布:微信ubuntu版 编辑:程序博客网 时间:2024/06/07 15:54
Python有一个很奇妙的特性,称为
文档字符串 ,它通常被简称为 docstrings 。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把每一样东西 都作为对象,包括这个函数。
如果你已经在Python中使用过
help()
,那么你已经看到过DocStings的使用了!它所做的只是抓取函数的__doc__
属性,然后整洁地展示给你。你可以对上面这个函数尝试一下——只是在你的程序中包括help(printMax)
。记住按q退出help
。
自动化工具也可以以同样的方式从你的程序中提取文档。因此,我
强烈建议 你对你所写的任何正式函数编写文档字符串。随你的Python发行版附带的pydoc命令,与 help()
类似地使用DocStrings。转自:http://www.cnblogs.com/jlsme/articles/1394003.html
- 转:python中的文档字符串(docStri…
- python中的文档字符串(docString)
- python中的文档字符串(docString)
- 理解python中的文档字符串
- python中的文档字符串(docString)
- Python中的文档字符串的作用
- python中的多行字符串及文档字符串
- Python 文档化字符串
- Python docstring文档字符串
- python的文档字符串
- 3.14 Python文档字符串
- python文档字符串
- python打印文档字符串
- 1. python 文档字符串
- Python中的值转字符串
- GoodZhang在学Python(五)--Python中的文档字符串(docStrings)
- 【python coding 6:文档字符串】Python的文档字符串DocStrings
- 英文文档中的字符串替换
- 转:驯服 Tiger: 深入研究枚举类型
- IO和Decorator模式
- 开始慢慢转到51cto
- 在Python中判断字符为数字还是字母
- Ubuntu 命令行学习目标
- 转:python中的文档字符串(docStri…
- hibernate hql异常
- python>>import this
- Android学习笔记: Android动画特效
- 转:《Python核心编程》第二版第三…
- appscan 下载
- python因为格式带来的问题
- 转:《Python核心编程》第二版第四…
- 突然想到的一个python判断小数方法