dir()
dir([object]) Returns:
list · Added in v3.0 · Updated March 13, 2026 · Built-in Functions introspection built-in debugging exploration
The dir() function is one of Python’s most useful introspection tools. It returns a list of all valid attributes and methods available on an object or in the current scope. When called without arguments, it returns names in the current local scope. When called with an object, it returns that object’s attributes.
Syntax
dir()
dir(object)
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
object | any | None | Optional. Any Python object. If omitted, returns names in the current local scope. |
Returns: A list of strings representing attribute names.
Examples
Basic usage — current scope
# dir() without arguments returns names in current scope
print(dir())
# [__builtins__, __doc__, __loader__, __name__, __package__]
Exploring an object’s attributes
# dir() on a string object
my_string = "hello"
attrs = dir(my_string)
print(len(attrs)) # 47 attributes on a string
# 47
# Filter to just public methods
public_methods = [a for a in dir(my_string) if not a.startswith(_)]
print(public_methods)
# [capitalize, casefold, center, count, encode, endswith,
# expandtabs, find, format, format_map, index, isalnum,
# isalpha, isascii, isdecimal, isdigit, isidentifier,
# islower, isnumeric, isprintable, isspace, istitle,
# isupper, join, ljust, lower, lstrip, maketrans,
# partition, removeprefix, removesuffix, replace, rfind,
# rindex, rjust, rpartition, rsplit, rstrip, split,
# splitlines, startswith, strip, swapcase, title,
# translate, upper, zfill]
Exploring a custom class
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def greet(self):
return f"Hello, I'm {self.name}"
# See all attributes of a Person instance
p = Person("Alice", 30)
attrs = dir(p)
# Check if an attribute exists
print(name in attrs)
# True
print(greet in attrs)
# True
print(email in attrs) # Not set
# False
Dynamic attribute access
class Calculator:
def add(self, a, b):
return a + b
def multiply(self, a, b):
return a * b
calc = Calculator()
# Find all callable methods dynamically
callable_attrs = [attr for attr in dir(calc)
if callable(getattr(calc, attr)) and not attr.startswith(_)]
print(callable_attrs)
# [add, multiply]
Common Patterns
Interactive exploration in REPL
# Use dir() to explore unknown modules
import json
print(dir(json))
# [JSONDecodeError, JSONDecoder, JSONEncoder, __all__, __author__,
# __builtins__, __cached__, __doc__, __file__, __loader__,
# __name__, __package__, __spec__, dump, dumps, load, loads]
# Explore what you can do with the module
functions = [name for name in dir(json) if not name.startswith(_)]
print(functions)
# [JSONDecodeError, JSONDecoder, JSONEncoder, dump, dumps, load, loads]
Docstring discovery
# Get docstrings for methods
my_list = [1, 2, 3]
print(my_list.append.__doc__)
# Append object to the end of the list.
print(my_list.sort.__doc__)
# Sort the list in ascending order and return None.
Errors
TypeError: Raised whendir()is called with more than one argument.
dir(1, 2)
# TypeError: dir expected at most 1 argument, got 2
See Also
- built-in::hasattr — Check if an object has a specific attribute
- built-in::vars — Return the
__dict__of an object - built-in::getattr — Get an attribute from an object by name