Ravindu Shantha Ravindu Shantha - 9 months ago 60
Python Question

python class and dir() function

I created an empty class in python and i put that class name to

dir()
(
dir(A)
) function then it returned output like this:

['__class__', '__delattr__', '__dict__', '__doc__', '__eq__',
'__format__', '__ge__', '__getattribute__', '__gt__', '__hash__',
'__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__',
'__reduce__', '__reduce_ex__', '__repr__', '__setattr__',
'__sizeof__', '__str__', '__subclasshook__', '__weakref__']


So I want to know from where those things came from??

Answer Source

Most of them comes from object, which all classes inherit from. But some are set when defining the new class.

set(dir(A)) - set(dir(object))

{'__dict__', '__module__', '__weakref__'}

So these three does not come from object.