What is the dir() function in Python?

Key takeaways:

  • The dir() function in Python returns a list of attributes and methods associated with an object, which is useful for exploring modules, objects, and elements.

  • When called without arguments, dir() returns names from the current local scope.

  • This syntax of this function is dir([object]), where the object parameter is optional.

  • It can be used for debugging, inspecting built-in modules, or analyzing user-defined objects.

  • Use dir() for quick inspections of objects or modules during development. It helps you verify available methods and attributes, especially when working with unfamiliar code or performing unit tests.

The dir() function is a built-in Python tool that returns a list of attributes and methods associated with an object. Assume you’re handed a module with no documentation—the dir() function becomes your first tool to uncover its capabilities, allowing you to quickly inspect its attributes and methods. It's especially useful when you’re working with unfamiliar modules, objects, or libraries and need to explore their properties. With dir(), you can efficiently navigate and understand the structure of any Python element, making it an essential tool for debugging and learning.

In this Answer, we’ll go over its usage, syntax, examples, and how we can use it in different scenarios.

Definition of the dir() function

The dir() function is used to return all the valid attributes of an object. When called without arguments, it returns the list of names in the current local scope. It’s useful for debugging and understanding what functions or variables an object contains.

Syntax of the dir() function

dir([object])
  • Parameter values

    • Object (optional): Any object whose attributes we want to inspect. If no object is passed, dir() returns the names in the current local scope.

  • Returns

    • The function returns a list of strings representing the names of the attributes and methods of the passed object. If no object is passed, it returns the names in the current scope.

Usage of dir()

The dir() function helps users understand what functions, methods, or attributes are available for a particular object. It can be used on built-in objects, modules, or user-defined objects to get a list of their properties.

Examples of the Python dir() function

Let’s explore how dir() works with different kinds of objects.

1. When no parameters are passed

When no parameter is passed to dir(), it returns all the names currently defined in the local scope.

x = 10
y = "Hello"
print(dir())

Try experimenting by adding, removing, or modifying the variables and learn how the output of dir() changes.

2. When a module object is passed

We can pass a module as an argument to dir() to see all the functions and variables within that module.

import math
print(dir(math))
3. When a list object is passed as a parameter

If you pass a list object, dir() will show you all the methods and properties of that list.

my_list = [1, 2, 3]
print(dir(my_list))
4. When user-defined objects are passed as parameters

You can also pass a user-defined object to dir() to see its attributes and methods.

class MyClass:
def __init__(self):
self.name = "Python"
def greet(self):
return "Hello, " + self.name
obj = MyClass()
print(dir(obj))

Difference between dir(), help(), and vars()

While dir() is great for quickly listing the attributes and methods of an object, it’s helpful to understand how it compares with other built-in functions like help() and vars(). Here’s a side-by-side comparison:

Function

Purpose

When to Use

Example

dir()

Lists all attributes and methods of an object

Use when you want to explore the object’s available attributes and methods or when debugging

dir(object)

help()

Provides detailed information about an object, including its docstring and usage

Use when you need a more in-depth explanation of an object, method, or class

help(object)

vars()

Returns the __dict__ attribute of an object, showing its attributes and values

Use when you need to inspect the attributes and their current values, often in the context of custom objects

vars(object)

Ready to deepen your Python knowledge? Start our "Become a Python Developer" Skill Path, which begins with Python’s basic concepts and advances to more complex topics, preparing you for a career as a Python developer.

Frequently asked questions

Haven’t found what you were looking for? Contact Us


How I can start Python programming?

To learn coding, start with interactive, hands-on platforms like Educative’s “Learn to Code” Skill Path. It offers step-by-step lessons with project-based content to help beginners gain practical skills and become job-ready programmers.


What is the use of help() and dir() functions in Python?

The help() function provides documentation about Python objects, while dir() lists all attributes and methods of an object. Both are used for introspection and exploring code in Python.


What is dir() and var() in Python?

The dir() function lists an object’s attributes and methods, while vars() returns the __dict__ attribute of an object, showing its symbol table.


What does the term “dir” mean in Python?

The term “dir” name often refers to the name of a directory in the file system. In Python, the os.path.dirname() function can extract the directory name from a given path.


Can dir() list the attributes added to an object at runtime?

Yes, dir() can list attributes added to an object at runtime. It dynamically reflects the object’s current state, including any attributes or methods that are added during execution.


Free Resources