luofeiyu_cu 发表于 2014-07-28 10:01

python的__doc__问题

好像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__的方式显示出来,或者其他的方式?

jun413947139 发表于 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为例 :#! /usr/bin/python
'''
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 *在python shell中执行>>> import mget
>>> mget.__doc__

icymirror 发表于 2014-07-31 13:02

回复 1# luofeiyu_cu
可以考虑使用自省。
import inspect
然后,使用inspect的get系列函数,可以得到对应的源文件。
之后,你可以根据需要过滤。
页: [1]
查看完整版本: python的__doc__问题