2013-05-03 192 views

回答

5

有关详细的文档访问online documentation

pydoc服务器。这是文档的离线版本,而不是一个详细的一个,但:

$ python -m pydoc -p 5555 

它在本地主机启动pydocs服务器,并可以将该链接访问文档。

为了快速查找,您可以使用dir()它会返回一个对象的所有属性:

>>> dir(object) 
['__class__', '__delattr__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__'] 
>>> dir(dict) 
['__class__', '__cmp__', '__contains__', '__delattr__', '__delitem__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'has_key', 'items', 'iteritems', 'iterkeys', 'itervalues', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values', 'viewitems', 'viewkeys', 'viewvalues'] 

有关属性的一些信息使用help()

>>>help(dict.get) 
get(...) 
    D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None. 

您也可以使用该模块pydoc

>>> import pydoc 

>>> print pydoc.getdoc(dict) 
dict() -> new empty dictionary 
dict(mapping) -> new dictionary initialized from a mapping object's 
    (key, value) pairs 
dict(iterable) -> new dictionary initialized as if via: 
    d = {} 
    for k, v in iterable: 
     d[k] = v 
dict(**kwargs) -> new dictionary initialized with the name=value pairs 
    in the keyword argument list. For example: dict(one=1, two=2) 

>>> print pydoc.getdoc(dict.get) 
D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None. 
+0

帮助(帮助):-) - – Ant 2013-05-03 20:20:47

0
>>> dir({}) 
['__class__', '__cmp__', '__contains__', '__delattr__', '__delitem__', '__doc__',  
'__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', 
'__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', 
'__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', 
'__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'has_key', 'items', 
'iteritems', 'iterkeys', 'itervalues', 'keys', 'pop', 'popitem', 'setdefault', 
'update', 
'values', 'viewitems', 'viewkeys', 'viewvalues'] 

同理:

>>> o = object() 
>>> dir(o) 
0

对于方法的列表,请尝试:

help(object) 

help(dict) 
1

真的是没有内置的类或别的什么区别。这些都是找出任何对象的详细信息的步骤:

首先尝试help()

>>> help(dict) 
Help on class dict in module __builtin__: 

class dict(object) 
| dict() -> new empty dictionary 
| dict(mapping) -> new dictionary initialized from a mapping object's 
|  (key, value) pairs 
| dict(iterable) -> new dictionary initialized as if via: 
|  d = {} 
|  for k, v in iterable: 
|   d[k] = v 
| dict(**kwargs) -> new dictionary initialized with the name=value pairs 
|  in the keyword argument list. For example: dict(one=1, two=2) 
| 
| Methods defined here: 
| 
| __cmp__(...) 
|  x.__cmp__(y) <==> cmp(x,y) 
... 

您还可以得到属性和方法的列表,dir()

>>> dir(list) 
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort'] 

你可以通过查看__dict__找到大部分数据:

>>> sys.__dict__ 
{'setrecursionlimit': <built-in function setrecursionlimit>, 
'dont_write_bytecode': False, 'getrefcount': <built-in function getrefcount>, 
'long_info': sys.long_info(bits_per_digit=15, sizeof_digit=2), 'path_importer_cache': 
{'': None, '/usr/lib/python2.7/encodings': None, 
'/usr/local/lib/python2.7/dist-packages/docutils-0.10-py2.7.egg': None, 
'/usr/lib/python2.7/plat-linux2': None, 
... 

虽然很多内置类型都没有,或者很少。

>>> 'foo'.__dict__ 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
AttributeError: 'str' object has no attribute '__dict__' 

有关更多说明,请访问documentation。欲了解更多详情,请阅读the source code

+0

我猜'pydoc'服务器也是一个不错的选择。 – 2013-05-03 20:50:42

+0

是的,我从来没有发现它非常有用,但这是我猜想的味道问题。 – 2013-05-03 21:24:42