Python: Difference between dir() and help()
Last Updated :
04 Mar, 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 |
Similar Reads
Difference between dir() and vars() in Python
As Python stores their instance variables in the form of dictionary belonging to the respective object both dir() and vars() functions are used to list the attributes of an instance/object of the Python class. Though having a similar utility these functions have significant individual use cases.dir(
2 min read
Difference between Logging and Print in Python
In Python, print and logging can be used for displaying information, but they serve different purposes. In this article, we will learn what is python logging with some examples and differences between logging and print in Python. Logging in Python Logging in Python is a technique to display useful m
3 min read
Difference between return and print in Python
In Python, we may use the print statements to display the final output of a code on the console, whereas the return statement returns a final value of a function execution which may be used further in the code. In this article, we will learn about Python return and print statements. Return Statement
2 min read
Difference between casefold() and lower() in Python
In this article, we will learn the differences between casefold() and lower() in Python. The string lower() is an in-built method in Python language. It converts all uppercase letters in a string into lowercase and then returns the string. Whereas casefold() method is an advanced version of lower()
2 min read
Difference between Method and Function in Python
Here, key differences between Method and Function in Python are explained. Java is also an OOP language, but there is no concept of Function in it. But Python has both concept of Method and Function. Python Method Method is called by its name, but it is associated to an object (dependent).A method d
3 min read
What Is The Difference Between .Py And .Pyc Files?
When working with Python, you might have come across different file extensions like .py and .pyc. Understanding the differences between these two types of files is essential for efficient Python programming and debugging. '.py' files contain human-readable source code written by developers, while '.
3 min read
Difference Between os.rename and shutil.move in Python
When it comes to renaming or moving files and directories, two commonly used methods are os. rename and shutil. move. While both can serve the purpose of renaming and moving, they have distinct differences in terms of functionality and use cases. In this article, we will explore these differences an
3 min read
Difference Between except: and except Exception as e
In Python, exception handling is a vital feature that helps manage errors and maintain code stability. Understanding the nuances of different exception-handling constructs is crucial for writing robust and maintainable code. This article will explore the difference between except: and except Excepti
5 min read
Difference between input() and raw_input() functions in Python
Developers often have a need to interact with users, either to get data or to provide some sort of result. Most programs today use a dialog box as a way of asking the user to provide some type of input. While Python provides us with two inbuilt functions to read the input from the keyboard. input (
4 min read
What is the difference between Python's Module, Package and Library?
In this article, we will see the difference between Python's Module, Package, and Library. We will also see some examples of each to things more clear. What is Module in Python? The module is a simple Python file that contains collections of functions and global variables and with having a .py exten
2 min read