Python: Difference between dir() and help()
Last Updated :
23 Jul, 2025
In Python, the dir() and help() functions help programmers understand objects and their functionality.
- dir() lists all the attributes and methods available for an object, making it easy to explore what it can do.
- help() provides detailed information about an object, including descriptions of its methods and how to use them.
Let's understand the difference between them in more detail with the help of suitable examples.
dir() in Python
dir() function in Python helps you see what an object contains. It returns a list of all the attributes and methods available for that object. If used without an argument, it shows the names of everything in the current scope. This function is especially useful for debugging and exploring objects. The results it returns depend on what you pass as an argument, and it generally falls into four categories.
- No argument: returns a list of names in the current local scope.
- Module object as an argument: returns a list of the module’s attributes.
- Type or class object: returns a list of its attributes, along with attributes from its base classes recursively.
- Other objects: returns a list of the object's attributes, its class attributes, and attributes from its base classes recursively.
Let's discuss some of the most common use cases with examples.
Inspecting the attributes of built-in types
The dir() function can be used to explore available methods and attributes of built-in types like str, list, or dict.
Python
print(dir(str)) # Lists all methods and attributes of the str class
Output['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getstate__', '__gt__', '__hash__...
Listing Object Attributes
We are using dir() function to look into the attributes of a user defined list.
Python
l = [1, 2, 3]
print(dir(l))
Output['__add__', '__class__', '__class_getitem__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getstate__', '...
Explanation: dir(l) method lists all the attributes and methods for list 'l', like ['append', 'clear', 'count', ...]. It's great for discovering what operations a list supports without going through docs.
Checking current scope
dir() function, when called without arguments, lists all names available in the current local scope, including variables, functions, and built-ins. This helps inspect what’s currently defined in the script.
Python
x = 10
def say_hello():
pass
print(dir()) # Lists all variables and functions in the current scope
Output['__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'say_hello', 'x']
help() in Python
help() function in Python provides detailed information about objects, modules, or functions. It retrieves documentation from docstrings and built-in references to explain how different components work. If used without an argument, it opens an interactive help session. If given an object, it displays specific details about it. Users can enter a keyword, topic, or module name to get information. To exit, simply type quit. The output of help() is generally divided into three main parts.
- No argument: the interactive help system starts on the interpreter console.
- String as argument: the string is looked up as the name of a module, function, class, method, keyword, or documentation topic, and a help page is printed on the console.
- Any other kind of object: a help page on the object is generated.
Let's discuss some of the most common use cases with examples.
Getting documentation for built-in types
Use help() to get detailed information about built-in classes like str, int, or list.
Python
help(str) # Displays detailed documentation of the str class
OutputHelp on class str in module builtins:
class str(object)
| str(object='') -> str
| str(bytes_or_buffer[, encoding[, errors]]) -> str
|
| Create a new string object from the given object. If enc...
Understanding function behavior
help() function can explain how a function works, its parameters, and return values.
Python
import math
help(math.sqrt) # Shows details about the sqrt() function in math module
OutputHelp on built-in function sqrt in module math:
sqrt(x, /)
Return the square root of x.
Viewing docstrings of user-defined classes
If a class or function has a docstring, help() displays it along with method details.
Python
class Sample:
"""This is a sample class."""
def method(self):
"""This is a sample method."""
pass
help(Sample) # Displays docstrings and method details of the class
OutputHelp on class Sample in module __main__:
class Sample(builtins.object)
| This is a sample class.
|
| Methods defined here:
|
| method(self)
| This is a sample method.
|
| -----------...
Difference Between dir() and help() in Python
Feature | dir() | help() |
---|
Purpose | Lists attributes and methods | Provides detailed documentation |
Output | A list of names (strings) | Text-based help or interactive mode |
Detail Level | Surface-level (what’s there) | In-depth (what it does) |
Use Case | Quick lookup of options | Learning usage and details |
Argument | Optional (defaults to scope) | Optional (defaults to help mode) |
Example Output | ['append', 'pop', ...] | "Return the length of an object" |
Perspective | Assists in solving effortful problems in a simple way | Assists in knowing about the new modules or other objects quickly |