Dir Function in Python

Python is an object-oriented programming language and because of that, we have to deal with objects and classes in many scenarios. we have to work according to the object’s structure and available functionality but to know that we must have the knowledge of class and this is a long process to provide the solution or short solution of this process we have a dir function in python which tells us about the structure and available functionality of the object.

Dir Function in Python

The dir() function is a built-in function in Python that returns a list of names in the current local scope or a specified object. It is used to introspect Python objects and find out what methods or attributes they have.

When called without any arguments, the dir function in python returns a list of names defined in the current local scope, including variable names, function names, and import names.

When called with an object as an argument, dir() returns a list of attributes and methods associated with that object. This can be useful for exploring the capabilities of an object, particularly if it is a custom object defined by a third-party library.

Dir function in python is a powerful introspection tool that can be used to explore the names and capabilities of objects in both the current scope and external libraries.

Syntax of Dir Function in Python

The dir function in python has the following syntax:

dir({object})

We just have to pass the object.

Parameters of Dir Function in Python

The dir function in python can work without passing any parameter but however, but we can pass one parameter.

  • Object: this is optional, To learn about all the specified names in the namespace, we are not obliged to supply any arguments. But, you must supply it as a parameter in order to discover the characteristics or qualities of a certain object. This object might be a module, function, list, string, dictionary, etc.

Return Type of Dir Function in Python

The dir() function in Python returns a list of the names (attributes and methods) defined in the specified object.

The return type of the dir() function is always a list (of strings) containing the names of the attributes and methods of the specified object.
The passed object can be of different types as shown below:

  • A list of names with all the acceptable characteristics and base attributes is returned if the input object is a class.
  • The names of all the attributes that are contained in that module are returned if the input object is a module or a library object.
  • The function, however, just returns a list of all the names in the current local scope if no inputs are sent to it.

Examples of Dir Functions in Python

In this section, we will look at various examples of dir functions in python with different types of objects passed like list, tuple, set, and user-defined class.

Example 1 of the Dir Function with a List

Below is the cod implementation of the above-mentioned example.

number1 = [21, 32, 43]


print(dir(number1))

number2 = [ ]


print(dir(number2))

Output

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

Explanation
In the above example, we have used the dir function in python which returns all the valid attributes of the given lists number1 and number2
Application

Example 2 Dir Functions in Python with Set

Below is the code and implementation of the above-mentioned example of a dir function in python.

number = {22, 35, 48, 21}

print(dir(number))

number1 =[ ]

print(dir(number1))

Output

['__and__', '__class__', '__cmp__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__iand__', '__init__', '__ior__', '__isub__', '__iter__', '__ixor__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__or__', '__rand__', '__reduce__', '__reduce_ex__', '__repr__', '__ror__', '__rsub__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__xor__', 'add', 'clear', 'copy', 'difference', 'difference_update', 'discard', 'intersection', 'intersection_update', 'isdisjoint', 'issubset', 'issuperset', 'pop', 'remove', 'symmetric_difference', 'symmetric_difference_update', 'union', 'update']
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']

Explanation
In the above example, we have used a set to pass in the dir function in python and then it shows all the available attributes of number and number1.

Example 3 Dir Functions in Python with Tuple

Below is the code and implementation of the above mentioned example of a dir function in python.

number = (22, 30, 41, 56)
print(dir(number))

number1 =[]  
print(dir(number1))

Output

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

Explanation
In the above example, we used the tuple with the dir function in python and get the corresponding result which returns all the valid results for the tuples in the code.

Example 4 Dir Functions in Python with User-Defined Class

Now we will discuss the code and implementation of the above-mentioned example of a dir function in python.

class Employee:
  def __dir__(self):
    return ['age', 'name', 'salary','gender']    
 
Manager = Employee()
print(dir(Manager))

Output

['age', 'gender', 'name', 'salary']

Explanation
In this example, we have used a user-defined class in the dir function in python the user-defined class is Employee and the object is Manger.
We have passed the age, gender, name, and salary as the attributes so dir will use them for output.

Applications of Dir Function in Python

Here are some common applications of the dir function in Python:

  • Inspecting module contents: You can use the dir function to inspect the contents of a Python module. For example, if you want to see what functions and variables are defined in the math module, you can use dir(math).
  • Exploring objects: You can use the dir function to explore the attributes and methods of any object. For example, if you want to see what methods and properties are available for a list object, you can use dir(list).
  • Debugging: You can use the dir function to help you debug your code. For example, if you’re not sure what methods are available for a certain object, you can use dir to see what’s available.
  • Learning: You can use the dir function to learn about new modules, classes, or objects. By exploring the attributes and methods available, you can gain a better understanding of how the module, class, or object works.
  • Completing code: You can use the dir function to help you complete your code. For example, if you’re trying to remember the exact name of a method or property, you can use dir to see what’s available.

Frequently Asked Questions

1. How do I use the dir function in Python?
To use the dir function in Python, simply call the function with the object you want to inspect as a parameter. For example, dir(math) will return a list of the attributes and methods of the math module.

2. What types of objects can I use the dir function within Python?
You can use the dir function with any object in Python, including modules, classes, functions, and instances of classes.

3. What is the output of the dir function in Python?
The dir function returns a list of strings, which represent the attributes and methods of the specified object.

4. How can I use the dir function to learn about a new module or object in Python?
To learn about a new module or object in Python, you can use the dir function to inspect its attributes and methods. This can give you a better understanding of how the module or object works and how to use it in your code.

5. Can I use the dir function to modify an object in Python?
No, the dir function only returns information about an object’s attributes and methods. It does not modify the object in any way.

Leave a Reply

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