0
0
PythonHow-ToBeginner · 3 min read

How to Use help() Function in Python: Simple Guide

Use the help() function in Python by passing a module, function, class, or object as an argument to get its documentation and usage details. For example, help(str) shows information about the string type.
📐

Syntax

The help() function is called with one argument, which can be a module, function, class, or object. It displays the documentation string (docstring) and other useful info about the argument.

  • help(object): Shows help for the given object.
  • If called without arguments, it starts an interactive help session.
python
help(object)
💻

Example

This example shows how to use help() to get information about the built-in list type and a user-defined function.

python
def greet(name):
    """Return a greeting message for the given name."""
    return f"Hello, {name}!"

help(list)
print('---')
help(greet)
Output
Help on class list in module builtins: class list(object) | list(iterable=(), /) | | Built-in mutable sequence. | | Methods defined here: | ... | | Data and other attributes defined here: | ... --- Help on function greet in __main__: greet(name) Return a greeting message for the given name.
⚠️

Common Pitfalls

Some common mistakes when using help() include:

  • Passing a string with the name of the object instead of the object itself (e.g., help('list') shows help for the string 'list', not the list type).
  • Expecting help() to work on objects without docstrings or documentation, which may show limited info.
  • Not realizing that help() output appears in the console or terminal, so it may pause the program until you exit the help viewer.
python
help('list')  # Shows help for string 'list', not list type
help(list)    # Correct usage

# Objects without docstrings
class Empty:
    pass
help(Empty)  # May show limited info
Output
Help on str object: class str(object) | str(object='') -> str | ... Help on class Empty in module __main__: class Empty(builtins.object) | | Data and other attributes defined here: | ...
📊

Quick Reference

Remember these tips when using help():

  • Pass the actual object, not its name as a string.
  • Use help() without arguments to enter interactive help mode.
  • Use it to explore built-in modules, functions, classes, and your own code.
  • Exit help by typing q when in the help viewer.

Key Takeaways

Use help() with the actual object to see its documentation and usage.
help() without arguments starts an interactive help session.
Avoid passing object names as strings to help(), pass the object itself.
help() is useful for exploring built-in and user-defined code quickly.
Exit the help viewer by typing 'q' to continue your program.