Python

Python dir() function

The dir() function of Python is used to retrieve the attributes and methods of any object like string, list, dictionary, function, class, module, etc. This function returns the list of attributes and methods of the standard library that is available after initializing the Python program. The purposes of using the dir() function and the different uses of the dir() function in Python have been shown in this tutorial.

Syntax:

The dir() function can be used with the argument and without the argument. It returns different types of attributes and methods based on the object used as the argument. The syntax of this function is given below.

dir([object])

  • If no argument is passed in this function, then the list of the names of the current local scope will be returned by this function.
  • If the class object is used as the argument, then the dir() function will return the list of all valid attributes.
  • If the module is used as the argument, then the dir() function will return the list of all attributes contained by that module.

Example-1: Use of dir() function without argument

Create a python file with the following script to check the returned value of the dir() function when it is used without any argument. In the script, the dir() function without argument is called before importing any module and after importing two modules.

#Print the output of dir() function before importing any module
print("The output of dir() function before import:\n", dir())

#Import modules
import sys
import os

#Print the output of dir() function after importing sys and os modules
print("\nThe output of dir() function after import:\n", dir())

Output:
The following output will appear after executing the above script. After importing the modules, the output shows that os and sys have been added to the dir() output.

Example-2: Use of dir() function for the string as an argument

Create a python file with the following script where the string object has been used as the argument of the dir() function. In this case, the dir() function will return the list of all attributes of the string object.

#Define a string value
text = 'LinuxHint'
#Print the output of dir() function for string value
print("\nThe output of dir() function for the string data:\n", dir(text))

Output:
The following output will appear after executing the above script.

Example-3: Use of dir() function for the list as the argument

Create a python file with the following script where the list object has been used as the argument of the dir() function. In this case, the dir() function will return the list of all attributes of the list object.

#Define a list of decimal numbers
numList = [6.7, 3.2, 8.0, 2.8, 9.1, 1.5, 0.9]
#Print the output of dir() function for the list
print("\nThe output of dir() function for the list of numbers:\n", dir(numList))

Output:
The following output will appear after executing the above script.

Example-4: Use of dir() function for the tuple as argument

Create a python file with the following script where the tuple object has been used as the argument of the dir() function. In this case, the dir() function will return the list of all attributes of the tuple object.

#Define a tuple of string
strTuple = ['Book', 'Pen', 'Pencil','Eraser','Ruler','Color Pencil']
#Print the output of dir() function for the tuple
print("\nThe output of dir() function for the tuple:\n", dir(strTuple))

Output:
The following output will appear after executing the above script.

Example-5: Use of dir() function for the dictionary as argument

Create a python file with the following script where the dictionary object has been used as the argument of the dir() function. In this case, the dir() function will return the list of all attributes of the dictionary object.

#Define a dictionary
dicData = {'8967':90, '4523':85, '9123':75,'6580':88}
#Print the output of dir() function for the dictionary
print("\nThe output of dir() function for the dictionary:\n", dir(dicData))

Output:
The following output will appear after executing the above script.

Example-6: Use of dir() function for the object of a class

Create a python file with the following script where the user-defined class object has been used as the argument of the dir() function. In this case, the dir() function will return the list of all attributes of the class object.

#Define a class with a constructor
class Client:

   def __init__(self, name, mobile, email):
      self.name = name
      self.mobile = mobile
      self.email = email

#Create an object of the class
objClient = Client('Amir Hossain','+8801937865645','[email protected]' )

#Print the dir() function output for the object
print("The output of the dir() function for the object:\n", dir(objClient))

Output:
The following output will appear after executing the above script.

Example-7: Use of dir() function for a particular module

Create a python file with the following script where the module has been used as the argument of the dir() function. The dir() function has been used with sys and randint modules in the script. The dir() function will return the list of all attributes of these modules as the output.

#Import sys module
import sys
#Import randint from random
from random import randint

#Print the output of dir() function for the sys
print("The output of the dir() for sys:\n",dir(sys))
#Print the output of dir() function for the randint
print("\nThe output of the dir() for randint:\n",dir(randint))

Output:
The following output will appear after executing the above script. The first output shows the attributes of the sys module, and the second output shows the attributes of the randint module.

Conclusion:

The uses of the dir() function without any argument and with the different arguments have been shown in this tutorial using multiple examples. I hope using the dir() function in python will be cleared after reading this tutorial.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.