Python dir() Function: Full Guide from Basics to Advanced

1. What is Python’s dir() function?

When learning Python, the dir() function is an important tool for improving development efficiency. Using this function, you can easily list the attributes and methods of an object. In this article, we will thoroughly explain Python’s dir() function, covering everything from basic usage to advanced examples and cautions. In development with Python, this function lets you see at a glance which attributes and methods are available, making it especially useful for beginners. Learn through concrete examples and take your skills to the next level.

2. dir()Function Basics [How to Use Python dir]

What is dir() function?

The dir() function is a built-in Python function that returns a list of the specified object’s attributes (variables) and methods (functions). If no argument is given, it returns the names defined in the current scope.

Basic Syntax

dir([object])
  • object (optional): The object whose attributes you want to retrieve. If omitted, it returns the names in the current scope.

When No Argument Is Provided

# Get the names in the current scope
print(dir())
As a result, the variables and functions available in the current scope are displayed as a list.

When an Argument Is Provided

You can view the attributes and methods contained in the object.
my_list = [1, 2, 3]
print(dir(my_list))
Example Output:
['__add__', '__class__', '__contains__', ..., 'append', 'clear', 'copy']
In this example, all methods that the list type possesses are displayed as a list.

3. How to use dir() function (Beginner-friendly)

dir() function is a very handy tool for Python beginners to understand the characteristics of new objects and data types. Let’s learn the basics through the examples below.

Example of using list objects

We examine what attributes and methods a list type has.
numbers = [1, 2, 3]
print(dir(numbers))
Methods such as append and clear that appear in the output are frequently used for list manipulation.

Example of using dictionary objects

It can also be used with dictionary objects.
my_dict = {"key": "value"}
print(dir(my_dict))
In this way, by using dir(), you can quickly check the characteristics of each object and apply them to real programming.

4. dir() Function Applications [Intermediate]

dir() function is also useful in intermediate-level scenarios. In particular, it is handy for exploring custom classes and external modules.

Utilizing dir() in Custom Classes

You can inspect the attributes of a class you defined.
class MyClass:
    def my_method(self):
        pass

obj = MyClass()
print(dir(obj))
The output includes my_method. Additionally, Python’s built-in attributes (e.g., __class__ and __dir__) are also listed.

Customizing the __dir__() Method

It is also possible to customize the output of dir().
class CustomClass:
    def __dir__(self):
        return ['custom_attribute']

obj = CustomClass()
print(dir(obj))
This approach allows you to limit the output to only the essential information.
侍エンジニア塾

5. Using dir() in Debugging

During debugging in development, the dir() function is extremely handy. It is especially useful when you want to inspect attributes of external modules or new classes.

Exploring External Modules

For example, list all functions of the math module.
import math
print(dir(math))
This way, you can see the available functions at a glance (e.g., sin, cos).

Example Use During Debugging

When an error occurs, checking what methods an object has can help you find a solution.

6. dir() function and comparison with other functions [A must‑read for Python developers]

help() function comparison

  • help(): Provides detailed documentation of the object.
  • dir(): Lists the object’s attributes and methods.
Using both together is more effective.
print(help(str))
print(dir(str))

vars() function differences

  • vars(): Returns the object’s attributes and their values as a dictionary.
  • dir(): Returns a list of attribute names.

7. Points to Note

  • The output of dir() does not include every attribute or method.
  • Hidden attributes (those starting with _ or __) are used as developer‑focused information.
  • It should be used as an auxiliary tool to deepen your understanding of Python object characteristics.

8. Master dir() in Python!

In this article, we explained the basic usage, advanced examples, and cautions of Python’s dir() function. By leveraging this function, you can efficiently understand object characteristics and improve development speed. Next, be sure to try dir() in actual code! If you have questions or feedback, we look forward to hearing them in the comments.