Python dir() function

Use Python's dir() function to list methods and attributes of various objects, making it easier to work with different data types at rrtutors.com.

Published February 15, 2022

In Python, dir() is a powerful inbuilt function that is used to return the lists of methods and attributes of any object in a program including, dictionaries, lists, strings, modules, functions, etc. when this function is called, and it returns the list of attributes of the selected object.

 

Syntax

The syntax of the dir() function includes:

Dir({object}),

where an object can be either a dictionary, string, list, module, etc.

 

Example

In this example, we are going  to display the list of the random library  content using the dir() function

import random

print("The random library content includes::")

print(dir(random))

 

Output

Python dir function

 

Related Tutorials & Resources