Python Tutorial Python Advanced Python References Python Libraries

Python dir() Function



The Python dir() function returns a list containing all properties and methods of the specified object. The function returns properties and methods of built-in objects as well as user-defined objects, without specifying their values.

Syntax

dir(object)

Parameters

object Required. Specify object to find out its defined properties and methods.

Example:

In the example below, a list called MyList is created. After that, the dir() function is used to find out all properties and methods of MyList.

MyList = [10, 20, 30]
x = dir(MyList)
print(x)

The output of the above code will be:

['__add__', '__class__', '__class_getitem__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']

❮ Python Built-in Functions