Python dir() Function Explained
The Python dir() function is a simple inspection tool. It helps you see what names are available on an object, module, or in the current scope.
This is especially useful when you are:
- learning Python
- exploring a new object
- debugging an
AttributeError - checking what methods exist on a value
In short, dir() helps you answer the question: “What can I use here?”
Quick example
name = "hello"
print(dir(name))
Use dir(object) to see the attributes and methods available on that object.
Example output will include names such as:
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', ..., 'lower', 'split', 'upper']
Notice that the result is a list of strings.
What dir() does
dir() shows the names available on an object.
It usually includes:
- attributes
- methods
- special Python names
Important points:
- The result is a list of strings
- Each string is the name of something available on that object
- It is often used for exploration and debugging
For example, if you want to know what you can do with a string, dir() gives you a list of available names.
Basic syntax
dir() can be used in two main ways.
dir() with no argument
This shows names in the current scope.
x = 10
name = "Sam"
print(dir())
This will show names such as x and name, along with other names available in that scope.
dir(object)
This shows names related to a specific object.
print(dir("hello"))
print(dir([1, 2, 3]))
Common objects you might inspect:
- strings
- lists
- modules
- custom objects
Using dir() with a value
A common use of dir() is passing in a value to see what methods it has.
Example: string methods
text = "hello"
print(dir(text))
You will see method names like:
loweruppersplitstrip
This tells you what operations are available for strings.
Example: list methods
numbers = [1, 2, 3]
print(dir(numbers))
You will see method names like:
appendpopremovesort
This is a good way to discover what you can do with a list.
If you are not sure what kind of object you have, type() can help first. Then dir() can show what names are available on that object.
Using dir() with no argument
When used with no argument, dir() shows names currently defined in your local scope.
This is useful when working in the Python shell.
x = 5
def greet():
return "Hello"
print(dir())
The output will include names like:
xgreet
This is helpful for checking:
- what variables exist
- what functions you have defined
- what names are currently available
It is especially useful while experimenting in the interactive Python shell.
How to read the output
When you first use dir(), the output can look overwhelming.
Example:
text = "hello"
print(dir(text))
You will probably see many names that start and end with double underscores, such as:
__class____doc____len__
These are special Python names used by the language itself.
As a beginner, it is usually better to focus first on regular method names such as:
upperlowersplitreplace
A key point to remember:
dir()lists names only- it does not explain what each name does
If you want an explanation of a method, use help().
For example:
name = "hello"
help(name.upper)
dir() on modules
You can also use dir() on modules after importing them.
This is useful when you want to explore what a module provides.
Example: math module
import math
print(dir(math))
You may see names like:
sqrtceilfloorpi
This helps you discover available functions and constants.
If you are exploring modules, these pages may help too:
Another example
import json
print(dir(json))
This can help you quickly spot useful names such as dump, dumps, load, and loads.
dir() vs help()
dir() and help() are often used together.
dir()
- shows available names
- helps you discover methods and attributes
help()
- explains what an object or function does
- shows usage details and documentation
A common beginner workflow looks like this:
name = "hello"
print(dir(name))
help(name.upper)
Use dir() first to find a method name.
Then use help() to understand how to use it.
Common beginner confusion
Here are a few things that often confuse beginners.
dir() returns names, not values
dir() does not give you the actual value of an attribute. It only gives you its name.
For example, seeing upper in dir("hello") does not run the method. It only tells you the method exists.
Not every name is for everyday use
Many names in the output are special Python internals, especially names with double underscores.
Beginners usually do not need most of these right away.
Output can vary
The exact output depends on the object type.
For example:
dir("hello")shows string-related namesdir([1, 2, 3])shows list-related namesdir(math)shows names from themathmodule
A name existing does not mean you know how to call it
Just because a method appears in dir() does not mean you already know:
- what it does
- what arguments it needs
- what it returns
That is why help() is the natural next step.
Common mistakes
Beginners often use dir() in these situations:
- Trying to understand what methods are available on a string, list, or dictionary
- Seeing an
AttributeErrorand wanting to check valid attribute names - Exploring a module without knowing its functions
- Using
dir()output without following up with documentation orhelp()
A simple debugging workflow is:
name = "hello"
print(type(name))
print(dir(name))
help(name.upper)
And for modules:
import math
print(dir(math))
You can try the same commands in the Python shell:
python
name = "hello"
dir(name)
help(name.upper)
import math
dir(math)
FAQ
What does dir() return in Python?
It returns a list of strings containing attribute and method names for an object, or names in the current scope when called with no argument.
What is the difference between dir() and help()?
dir() lists available names. help() gives documentation and usage details.
Why do I see names with double underscores in dir() output?
Those are special Python attributes and methods used internally by the language.
Can dir() help fix AttributeError?
Yes. It can show which attribute names actually exist on an object. This makes it useful when debugging an error like object has no attribute.
See also
- Python
help()function explained - AttributeError: object has no attribute fix
- Python modules explained
- Python math module overview
- Python
type()function explained
A good habit is to use dir() together with help() in the Python shell. It is one of the fastest ways to explore objects and learn Python methods as you go.