Open In App

Python: Difference between dir() and help()

Last Updated : 08 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In Python two commonly used functions for exploring objects, modules, and their attributes are dir() and help(). While both are useful for gaining insights into Python code, they serve distinct purposes. This article aims to clarify the differences between dir() and help() and guide developers on when and how to use each effectively.

What is dir() in Python

It is one of the built-in functions that is available in Python which has one optional argument. This built-in function will return the list of names in the current local scope. The dir() function syntax is dir(object). The object can be a function, module, string, tuple, dictionary, etc.

The dir() function is considered one of the functions that is useful in the debugging process. The return value from this function is different based on the argument passed. There are four types of return values in the dir() function:

  • No argument: return the list of names in the current local scope.
  • Module object as argument: return the list containing the names of the module’s attributes.
  • Type or class object: return the list containing the names of its attributes, and recursively of the attributes of its bases.
  • Other object: return the list containing the object’s attributes’ names, the names of its class’s attributes, and recursively of the attributes of its class’s base classes.

Python dir() Examples

Example 1: dir() Without Argument

In this example, we are using dir() without any arguments.

Python3




# Python code to perform dir()
# No argument is passed in the dir()
 
print(dir())


Output

[‘__annotations__’, ‘__builtins__’, ‘__cached__’, ‘__doc__’, ‘__file__’, ‘__loader__’, ‘__name__’, ‘__package__’, ‘__spec__’]

Example 2: dir() With Argument – list() function

In this example, we are using dir() function with list() function.

Python3




# Python code to perform dir()
# The list function is passed as the argument
 
print(dir(list))


Output

[‘__add__’, ‘__class__’, ‘__contains__’, ‘__delattr__’, ‘__delitem__’, ‘__dir__’, ‘__doc__’, ‘__eq__’, ‘__format__’, ‘__ge__’, ‘__getattribute__’, ‘__getitem__’, ‘__gt__’, ‘__hash__’, ‘__iadd__’, ‘__i…

Example 3: dir() With Argument – Person class

In this example, we are using dir() for a class object.

Python3




# Python code to perform dir()
# The Person class is passed as the argument
 
class Person:
    name = "Jane"
    age = 20
 
print(dir(Person))


Output

[‘__class__’, ‘__delattr__’, ‘__dict__’, ‘__dir__’, ‘__doc__’, ‘__eq__’, ‘__format__’, ‘__ge__’, ‘__getattribute__’, ‘__gt__’, ‘__hash__’, ‘__init__’, ‘__init_subclass__’, ‘__le__’, ‘__lt__’, ‘__modul…

What is help() Fuction in Python

help() function is another Python’s built-in function. Similar to dir(), the help() function also has one optional argument, which means it is not mandatory to pass the argument. By using help() function, it is easy to receive assistance in building Python programs.

The users need only input the name of any keyword, topic, or module. To exit or go back to the interpreter, simply type quit. The return values of help() functions are divided into three main parts as follow:

  • 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.

Python help() Function Example

Example 1: help() Without Argument

In this example, we are using help() without any arguments.

Python3




# Python code to perform help()
# No argument is passed in this help()
 
help()


Output

Welcome to Python 3.7's help utility!

If this is your first time using Python, you should definitely check out
the tutorial on the Internet at https://docs.python.org/3.7/tutorial/.

Enter the name ...

Example 2: help() With Argument – sum() function

In this example, we are using help() function with sum() as an argument.

Python3




# Python code to perform help()
# The sum function is passed as the argument
 
help(sum)


Output

Help on built-in function sum in module builtins:

sum(iterable, start=0, /)
    Return the sum of a 'start' value (default: 0) plus an iterable of numbers
    
    When the iterable is empty, return ...

Example 3: help() with argument – float data type

In this example, we are using help() with float as argument.

Python3




# Python code to perform help()
# The float data type is passed as the argument
 
help(float)


Output

Help on class float in module builtins:

class float(object)
 |  float(x=0, /)
 |  
 |  Convert a string or number to a floating point number, if possible.
 |  
 |  Methods defined here:
 |  
 |  __ab...

Difference Between dir() and help() in Python

Python dir()

Python help()

The syntax is dir(object)

The syntax is help(request)

If no argument is given, it returns the list of names in the current local scope.

If no argument is given, it returns an interactive help system on the interpreter console

if argument is given, it returns a list of valid attributes for that object.

If argument is given, it returns its documentation along with modules, keywords, and attributes

To get the result, we need to use the print() function

Example: print(dir(tuple))

To get the result, we can directly call the function

Example: help(tuple)

An interactive console is not achievable using dir()

An interactive console is achievable using help()

Its given information has different usage than the Python documentation

Its given information has similar usage to the Python documentation

Assists in solving effortful problems in a simple way

Assists in knowing about the new modules or other objects quickly



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads