Reading and Understanding Error Messages
but that is much uglier and error-prone. Second, the conversion to string would be done automatically by the format method instead of the explicit conversion to strings needed in this case. Third, when using the format method, we can change the message without having to deal with the variables used and vice-versa.
A Documentation Tool at the Prompt
When you are learning Python or working in the interpreter, you will encounter functions, statements, and objects you do not yet understand. Python provides a built-in help system so you can retrieve documentation without leaving the interpreter. This makes help() useful when you need a quick explanation while you are actively coding.
Python's built-in help() function provides immediate access to documentation about functions, statements, and objects without leaving the interpreter.
Use help() when you need a focused lookup. It keeps you in the interpreter and does not require an internet connection.
Two Ways to Ask for Help
The help system has two important modes. In the first mode, you pass an object name, commonly a function name written as a string. Python displays documentation about that object and then returns to the prompt automatically. In the second mode, you call help() with no argument. Python enters an interactive help interface in which you can browse and search for topics. This mode remains active until you press q.
| Call | Mode | How you leave it |
|---|---|---|
| help('function_name') | Quick documentation lookup | Returns to the prompt automatically |
| help() | Interactive browsing and searching | Press q |
Looking Up a Named Function
A quick documentation lookup
You need documentation about a particular Python function while remaining in the interpreter.
Name the function: Write the function name as a string argument to help().
Read the documentation: Python displays documentation about that function.
Continue coding: After displaying the documentation, this form returns you to the Python prompt automatically.
Use help('function_name') when you want a quick answer about a specific function.
Entering and Leaving Interactive Help
Calling help() with no argument changes the interaction from the ordinary Python prompt to an interactive help interface. In that interface, you can browse and search documentation for topics. Unlike a named lookup, this mode does not return to the prompt automatically. Press q to exit interactive help and return to the Python interpreter.
Learning About help Itself
The help system is self-documenting. You can use help() to enter interactive mode and search for information about the help system, its commands, and how to navigate it. This means that the tool can provide guidance about its own use instead of requiring you to remember every command before you begin.
Using the tool to understand the tool
You want information about Python's own help function.
Request help about help: Pass help as the object whose documentation you want to inspect.
Read the displayed information: Python displays documentation about the help function.
Compare the modes: Remember that help(help) is a lookup about a named object, while help() with no argument enters interactive help mode.
help(help) demonstrates that Python's help system can document the help function itself.
Mistakes Beginners Make
Assuming help() with no argument behaves like a quick lookup
The no-argument form enters interactive help mode and remains there while you browse or search.
Fix:
Press q to exit interactive help and return to the Python interpreter.Confusing a named lookup with interactive mode
A named argument requests documentation about a specific object, while no argument starts interactive browsing and searching.
Fix:
Use help('function_name') for a focused lookup and help() for interactive exploration.Thinking help requires leaving the interpreter
Python provides built-in documentation about functions, statements, and other objects directly at the prompt.
Fix:
Try the appropriate help() form first when you need a quick answer.
Use help() for quick, specific questions while you are working interactively. For detailed tutorials, examples, or broader conceptual explanations, online documentation or tutorials may be more appropriate.
Practice the Decision
Choose the appropriate help() form for each situation: you want a quick lookup about one function; you want to browse and search documentation; you want information about the help function itself.
Hints
- A named object produces a focused documentation lookup.
- No argument enters interactive help mode.
- The help function can itself be supplied as the object.
- The key decision is whether you need one object's documentation or an interactive search. A named call such as help('function_name') displays documentation and returns to the prompt. A no-argument call, help(), enters interactive help mode, where q returns you to the interpreter. The self-referential call help(help) lets the help system document itself.
Key Takeaways
- Python's built-in help() function provides documentation about functions, statements, and other objects inside the interpreter.
- Use help('function_name') for a focused lookup that returns to the prompt automatically.
- Use help() with no argument to browse and search interactively.
- Press q to leave interactive help and return to the Python prompt.
- Use help(help) to learn about Python's help function and its capabilities.