Reputation: 29079
where do I find the documentation for classes like object or dict ? I would like to know which methods they have and what attributes. I found most stuff at http://docs.python.org/2 but I could not find methods and attributes for class object.
Upvotes: 1
Views: 169
Reputation: 251096
For detailed docmentation visit online documentation.
pydoc
server. It's an offline version of documentation, not a detailed one though :
$ python -m pydoc -p 5555
It starts a pydocs server at localhost and you can access docs on that link.
For quick lookup you can use dir()
it will return all the attributes of an object:
>>> 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']
For some info on an attribute use help()
>>>help(dict.get)
get(...)
D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.
You can also use the module 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.
Upvotes: 5
Reputation: 172309
There really isn't any difference between the built in classes or anything else. These are the steps to find out more info for any object:
First try 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)
...
You can also get a list of attributes and methods with 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']
And you can find much of it's data by looking at the __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,
...
Although many built-in types doesn't have on, or have very little in it.
>>> 'foo'.__dict__
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'str' object has no attribute '__dict__'
And for more explanation, visit the documentation. For even more details, read the source code.
Upvotes: 1
Reputation: 673
>>> 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']
Similarly:
>>> o = object()
>>> dir(o)
Upvotes: 0