How to Use the Dir() and Help() Functions in Python

Python is a high-level, interpreted scripting language that is easy to learn and implement. It comes with a variety of built-in functions that facilitate many activities in programming.

In this tutorial, we are going to focus on two powerful built-in functions in Python: the dir() and help() functions. These tools are incredibly useful when you’re discovering new modules or trying to understand unfamiliar code.

What is the dir() Function?

The dir() function in Python is a powerful built-in function that returns a sorted list of attributes and methods belonging to an object. If you want to see what functions or attributes are available for use with an object, the dir() function can be incredibly helpful.

How to Use the dir() Function

Let’s see it in action. Below is a short code snippet that demonstrates the use of dir().

The output will be a list of all the attributes/methods of a string object.

['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']

What is the help() Function?

While the dir() function is great for discovering what attributes or methods are available, the help() function in Python is used to display the documentation of modules, functions, classes, etc., and comes very handy when we want to know how a function or module works.

How to Use the help() Function

Let’s see an example of using the help() function below:

This will output the documentation for the sorted() function:

Help on built-in function sorted in module builtins:

sorted(iterable, /, *, key=None, reverse=False)
    Return a new list containing all items from the iterable in ascending order.

    A custom key function can be supplied to customize the sort order, and the
    reverse flag can be set to request the result in descending order.

Full Code

Let’s put it all together. This is the full example of how to use dir() and help() functions:

Conclusion

The dir() and help() functions are powerful tools that can help you understand Python code more effectively. With these functions, you can understand the available commands for an object, as well as detailed documentation on its usage.

Understanding how to use these functions is an essential part of becoming a proficient Python programmer.