Python dir() function

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

 

Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

238 Views