Jul 4, 2011

python 获得帮助

如何获得帮助?以os.path为例
1. 直接输入 os.path,会有提示。(注意,如果对象是函数,后面不带 (),如带 (),表示调用。)

2. os.path.__doc__ 如果模块,类,函数以 '''some words''' 开头,则为 __doc__。__doc__ 是作者编写的。
假设 mod.fun, mod.__doc__ 不包含 mod.fun.__doc__

3. dir(os.path)
dir 是一个非常好用的函数,它会列出 os.path 的所有 attributes
dir(1) 列出1的名字,这个比较变态
dir('')列出str的名字
dir([])列出list的名字
dir({})列出dict的名字

也可用
dir(str)
dir(dict) 等

dir 配合 help 使用,是获得帮助的最佳组合。

4.
__class__ 类别

>>> os.path.__class__
<type 'module'>

__builtins__

5. pydoc help()
help() 根据 .py 文件生成帮助,不是用户编写的。

'''It a simple python module
'''
class C:
    '''A class'''
    def g():
        '''g function in C'''
        pass
    num = 3

def f():
    '''A function'''
    return 1

NAME
    x - It a simple python module

FILE
    e:\python\x\x.py

CLASSES
    C

    class C
     |  A class
     |
     |  Methods defined here:
     |
     |  g()
     |      g function in C
     |
     |  ------------------------------------------
     |  Data and other attributes defined here:
     |
     |  num = 3

FUNCTIONS
    f()
        A function


dir(__builtins__)

ArithmeticError
AssertionError
AttributeError
BaseException
BufferError
BytesWarning
DeprecationWarning
EOFError
Ellipsis
EnvironmentError
Exception
False
FloatingPointError
FutureWarning
GeneratorExit
IOError
ImportError
ImportWarning
IndentationError
IndexError
KeyError
KeyboardInterrupt
LookupError
MemoryError
NameError
None
NotImplemented
NotImplementedError
OSError
OverflowError
PendingDeprecationWarning
ReferenceError
RuntimeError
RuntimeWarning
StandardError
StopIteration
SyntaxError
SyntaxWarning
SystemError
SystemExit
TabError
True
TypeError
UnboundLocalError
UnicodeDecodeError
UnicodeEncodeError
UnicodeError
UnicodeTranslateError
UnicodeWarning
UserWarning
ValueError
Warning
WindowsError
ZeroDivisionError
_
__debug__
__doc__
__import__
__name__
__package__
abs
all
any
apply
basestring
bin
bool
buffer
bytearray
bytes
callable
chr
classmethod
cmp
coerce
compile
complex
copyright
credits
delattr
dict
dir
divmod
enumerate
eval
execfile
exit
file
filter
float
format
frozenset
getattr
globals
hasattr
hash
help
hex
id
input
int
intern
isinstance
issubclass
iter
len
license
list
locals
long
map
max
memoryview
min
next
object
oct
open
ord
pow
print
property
quit
range
raw_input
reduce
reload
repr
reversed
round
set
setattr
slice
sorted
staticmethod
str
sum
super
tuple
type
unichr
unicode
vars
xrange
zip

0 comments: