author avatar

adithya.hebbar

Tue Dec 03 2024

In Python, dir() lists the attributes and methods of an object, such as a class or instance.

Example:


class MyClass:
    class_variable = "Class Variable"

    def __init__(self):
        self.instance_variable = "Instance Variable"

    def my_method(self):
        pass

obj = MyClass()
print(dir(obj))


Output:
dir(obj) shows a list of attributes (class_variable, instance_variable) and methods (my_method), along with special methods (e.g., __init__). It helps explore what’s available in an object.