我在哪裏可以找到類似對象或字典的文檔?我想知道他們有哪些方法和屬性。我在http://docs.python.org/2找到了大部分東西,但我找不到類對象的方法和屬性。我在哪裏可以找到Python類?
回答
有關詳細的文檔訪問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.
>>> 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)
對於方法的列表,請嘗試:
help(object)
即
help(dict)
真的是沒有內置的類或別的什麼區別。這些都是找出任何對象的詳細信息的步驟:
首先嚐試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。
我猜'pydoc'服務器也是一個不錯的選擇。 – 2013-05-03 20:50:42
是的,我從來沒有發現它非常有用,但這是我猜想的味道問題。 – 2013-05-03 21:24:42
- 1. 我在哪裏可以找到InlinePageParser類?
- 2. 我在哪裏可以找到ServerConnection類?
- 3. 我在哪裏可以找到Base64Encoder類?
- 4. 我在哪裏可以找到dict_keys類?
- 5. 我在哪裏可以找到XrmServicesContext類?
- 6. 我在哪裏可以找到python抽象類型表?
- 7. 我在哪裏可以找到IVCWizCtlUI
- 8. 我在哪裏可以找到gtkmm/overlay.h?
- 9. 我在哪裏可以找到ExpressionParseHelper?
- 10. 我在哪裏可以找到ContactsListActivity?
- 11. 我在哪裏可以找到PyBluez API
- 12. 我在哪裏可以找到Psychtoolbox-3.0.11?
- 13. 我在哪裏可以找到System.ComponentModel.Composition.Initialization.dll?
- 14. 我在哪裏可以找到sharepoint api
- 15. 我在哪裏可以找到PHPUnit_Extensions_SeleniumTestCase?
- 16. 我在哪裏可以找到System.Linq.Dynamic dll?
- 17. 我在哪裏可以找到$腳本?
- 18. 我在哪裏可以找到tomesh.c?
- 19. Hrez.exe我在哪裏可以找到它?
- 20. 我在哪裏可以找到Microsoft.IdentityModel.Extensions.dll庫?
- 21. 我在哪裏可以找到System.Web.HttpContext.Current.Server dll?
- 22. 我在哪裏可以找到Google+ API?
- 23. 我在哪裏可以找到VAT API?
- 24. 我在哪裏可以找到com.ibm.xml.parsers.SAXParser?
- 25. 我在哪裏可以找到「j_security_check」?
- 26. 我在哪裏可以找到IContentTypeManager
- 27. 在哪裏可以找到我的DB2
- 28. 我在哪裏可以找到包javax.media.opengl?
- 29. 我在哪裏可以找到CSharpEvaluator?
- 30. 我在哪裏可以找到16F877A.h?
幫助(幫助):-) - – Ant 2013-05-03 20:20:47