Python dir() Function

Python Built In Dir() Function

This is a detailed tutorial of the Python built-in dir() Function. Learn to get the various attributes of a given object using this function with examples.

Python dir() Function

The function returns a python list of valid attributes of a given Python object. This list so returned by this function contains the name of the attributes and not the attributes themselves.

Note. Valid attributes refer to all of the properties and methods of the object.

Syntax

The following syntax illustrates the usage of the dir() function.

dir() Parameters

This function only takes a single argument and that is the object for which you want to find out the list of attributes. This is an optional parameter.

dir() Return Value

This function returns a list of attribute names for a given object if the object has been provided as the argument.

  • In case, the object’s class has the method __dir__() defined within itself is called and it also must return the list of attributes.
  • In case, the class belonging to the given object does not have the method __dir__() defined, then this function will try to find the attributes with the help of the __dict__ attribute (Again, if it’s defined) or from the type object. In such cases, the list of attributes, sometimes may not be complete.

If no object is passed as the argument to this function, then it returns the list of names of all of the attributes that are currently in the local scope.

Examples

Three different examples given below illustrate the working of the Python built-in dir() function for different objects.

Example 1. Basic usage of the dir() function

Here we’re finding the attribute names for a list and an integer object.

#List Object
cities = ["Ludhiana", "Chandigarh", "New Delhi"]

#Finding attributes of the list object, cities
listAttributes = dir(cities)
print(listAttributes)
print(len(listAttributes))

#Integer Object
count = 45

#Finding attributes of the integer object, count
integerAttributes = dir(count)
print(integerAttributes)
print(len(integerAttributes))

Output.

As you can see in the following output, the dir() function applied on the list object returns a list containing the names of its 45 attributes while the same function applied on an integer number returned a list of 69 attribute names.

We have used the built-in function len() to find the number of items contained in the list object.

Example 2. Finding attributes of User-Defined Class Objects

#User-defined Class with __dir__()
class Fruit:
    def __init__(self):
        print("Fruit Object Created!")
    def __dir__(self):
        return ["Color", "Season", "Taste"]

#User-defined Class without __dir__()
class Vegetable:
    def __init__(self):
        print("Vegeatable Object Created!")
        
mango = Fruit()
onion = Vegetable()

print(dir(mango))
print(dir(onion))

Output.

This shows that if the object class would have the __dir__() method defined then this function will return whatsoever will be returned by that method only. But make sure __dir__() method should only return a valid python list object.

On the other hand, if the object class does not have the __dir__() method, then the function finds all of the attributes itself.

Example 3. Applying dir() on Python Module Objects

In this example, we’re finding the attributes of the two built-in modules, the math, and the random module.

import math
import random

mathAttributes = dir(math)
print(mathAttributes)
print(len(mathAttributes))

randomAttributes = dir(random)
print(randomAttributes)
print(len(randomAttributes))

Output.

I hope you found this guide useful. If so, do share it with others who are willing to learn Python and other programming languages. If you have any questions related to this article, feel free to ask us in the comments section.

And do not forget to subscribe to WTMatter!

Subscribe To WTMatter!

Receive updates of our latest articles via email. Enter your email address below to get started.

Leave a Reply

Your email address will not be published. Required fields are marked *