Can anybody explain to me with adequate examples whats the difference b/w
>>> import inspect
>>> inspect.getmembers(1)
and
>>> type(1).__dict__.items()
and
>>> dir(1)
except that they show an decreasing no.s of attributes & methods in that order.
1 is integer (but it can be of any type.)
EDIT
>>>obj.__class__.__name__ #gives the class name of object
>>>dir(obj) #gives attributes & methods
>>>dir() #gives current scope/namespace
>>>obj.__dict__ #gives attributes
dir()allows you to customize what attributes your object reports, by defining__dir__().From the manual, if
__dir__()is not defined:This is also what
inspect.getmembers()returns, except it returns tuples of(name, attribute)instead of just the names.object.__dict__is a dictionary of the form{key: attribute, key2: atrribute2}etc.object.__dict__.keys()has what the other two are lacking.From the docs on
inspect.getmembers():For
int.__dict__.keys(), this isTo summarize,
dir()andinspect.getmembers()are basically the same, while__dict__is the complete namespace including metaclass attributes.