I would like to know the what are the methods and properties in the object class of Python but I can't find the documentation anywhere. Do you guys know where can I find it?
1 Answer
You can use dir to list them:
>>> dir(object)
['__class__',
'__delattr__',
'__dir__',
'__doc__',
'__eq__',
'__format__',
'__ge__',
'__getattribute__',
'__gt__',
'__hash__',
'__init__',
'__init_subclass__',
'__le__',
'__lt__',
'__ne__',
'__new__',
'__reduce__',
'__reduce_ex__',
'__repr__',
'__setattr__',
'__sizeof__',
'__str__',
'__subclasshook__']
Be warned that an object can lie about what attributes are there. For example, object.mro exists but the dir chooses not to tell you about it.
To find an "uncensored" list, try this:
>>> object.__dir__(object)
3 Comments
Martijn Pieters
dir() doesn't show methods provided by type, the metaclass of object. object.mro is really type.mro.Martijn Pieters
As for
object.__dir__() vs. dir(), the latter calls type.__dir__(object), while object.__dir__() is meant for instances (so used for dir(instance)). The latter also looks at type(instance), while the former does not, which is why the object.__dir__(object) list is larger. You basically have the same output as list(set(dir(object)).union(dir(type))) (modulus ordering).wim
As documented in the link provided. Note Because dir() is supplied primarily as a convenience for use at an interactive prompt, it tries to supply an interesting set of names more than it tries to supply a rigorously or consistently defined set of names, and its detailed behavior may change across releases. For example, metaclass attributes are not in the result list when the argument is a class.
ipythonwith its awesome tab completion.