免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2306 | 回复: 2
打印 上一主题 下一主题

python的__doc__问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2014-07-28 10:01 |只看该作者 |倒序浏览
好像python得__doc__,只能导出class或者function或者method里面用'''     '''   ,包裹起来的部分,某个模块开头的注释部分,可否在python console状态下,抓取出来?
例如

# mget.py
#
# by Nelson Rush
#
# MIT/X license.
#
# A simple program to download files in segments.
#
# - Fixes by David Loaiza for Python 2.5 added.
# - Nelson added fixes to bring the code to 2.7 and add portability between Windows/Linux.
# - The output of segment information has been corrected and cleaned up.
# - In outside cases where the client instances were not being closed, they are now closed.
#
import sys
import os
import asyncore
import socket
import platform
from string import *
from math import *
from time import *
from mmap import *

上面是一个mget.py的开头部分,从第一行到第13行的部分,可否在交互模式下,用  .__doc__的方式显示出来,或者其他的方式?

论坛徽章:
1
CU大牛徽章
日期:2013-03-14 14:08:55
2 [报告]
发表于 2014-07-31 11:25 |只看该作者
本帖最后由 jun413947139 于 2014-07-31 11:25 编辑

1. python 中__doc__是第一个未赋值的字符串(docstring),并不是注释, 所以你用# 开头的注释是不行的
2. __doc__ 支持 python的modules, functions, classes, and methods
3.  以你的mget.py为例 :
  1. #! /usr/bin/python
  2. '''
  3. mget.py
  4. by Nelson Rush

  5. MIT/X license.

  6. A simple program to download files in segments.

  7. - Fixes by David Loaiza for Python 2.5 added.
  8. - Nelson added fixes to bring the code to 2.7 and add portability between Windows/Linux.
  9. - The output of segment information has been corrected and cleaned up.
  10. - In outside cases where the client instances were not being closed, they are now closed.
  11. '''

  12. import sys
  13. import os
  14. import asyncore
  15. import socket
  16. import platform
  17. from string import *
  18. from math import *
  19. from time import *
  20. from mmap import *
复制代码
在python shell中执行
  1. >>> import mget
  2. >>> mget.__doc__
复制代码

论坛徽章:
4
白羊座
日期:2013-11-05 10:26:09冥斗士
日期:2015-11-17 14:19:55白银圣斗士
日期:2015-11-17 15:13:0815-16赛季CBA联赛之新疆
日期:2016-04-01 09:10:58
3 [报告]
发表于 2014-07-31 13:02 |只看该作者
回复 1# luofeiyu_cu
可以考虑使用自省。
import inspect
然后,使用inspect的get系列函数,可以得到对应的源文件。
之后,你可以根据需要过滤。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP