2013-05-03 189 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

相關問題