我在哪里可以找到Python类?

时间:2013-05-03 20:16:36

标签: python

在哪里可以找到对象 dict 等类的文档?我想知道他们有哪些方法以及有哪些属性。我在http://docs.python.org/2找到了大多数内容,但我找不到类对象的方法和属性。

4 个答案:

答案 0 :(得分:5)

有关详细说明,请访问online documentation

pydoc 服务器。它是文档的离线版本,但不是详细的文档:

$ python -m pydoc -p 5555

它在localhost启动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.

答案 1 :(得分: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

答案 2 :(得分: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)

答案 3 :(得分:0)

有关方法列表,请尝试:

help(object) 

help(dict)
相关问题