In Python, __dict__
and __dir__
are special attributes and methods that provide information about objects and classes.
__dict__
:
The __dict__
attribute is a dictionary that stores the attributes and methods of an object. Each object in Python has its own __dict__
that contains the object's namespace - a mapping of attribute names to their corresponding values. When you access an attribute of an object using dot notation (e.g., object.attribute
), Python looks up the attribute in the object's __dict__
.
For example:
class MyClass:
def __init__(self, x):
self.x = x
obj = MyClass(42)
print(obj.__dict__) # Output: {'x': 42}
In this example, obj.__dict__
contains the attribute x
with the value 42
.
__dir__
:
The __dir__
method is used to retrieve a list of valid attributes and methods that are available for an object or a class. When you call the dir()
built-in function without any arguments, it internally calls the object's __dir__
method to provide a list of attributes and methods that can be accessed on that object.
For example:
class MyClass:
def my_method(self):
pass
obj = MyClass()
print(dir(obj))
# Output: ['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', ... 'my_method']
In this example, the dir(obj)
call returns a list of attributes and methods available for the obj
object, including the special methods and attributes like __class__
, __delattr__
, __dict__
, __dir__
, and the custom method my_method
.
In summary, __dict__
is an attribute that holds a dictionary of an object's attributes and their values, while __dir__
is a method that provides a list of valid attributes and methods for an object or class.