Online Learning - Programming Languages - Python - Tech tutorial

Python help(): I Don’t Understand This Object, Can Somebody help() Me?

Python has a number of built-in functions that are incredibly useful and powerful. Just about everyone who has written even a line of Python is familiar with the print() function, but in this post, we’ll discuss one that is a bit less familiar, but still powerful: Python’s built-in help() function. According to the official documentation, help() is a function that invokes the built-in help system and importantly, is intended for interactive use. This  means you are likely never going to see it invoked in scripts, but it can be very useful when we’re using our read-eval-print loop (REPL) for experimentation! For those new to Python, or those who have forgotten what a REPL is, it’s the “shell” we get when we type the python command into our terminal.

Why Would I Use help()?

In a world with powerful search engines, technical forums like StackOverflow and Reddit, and seemingly ubiquitous access to information and opinions, our old friend help() can seem a bit antiquated. Why would we ever use a command-line application to get information when the internet is at our fingertips?

  1. We can trust help() – all of the information returned by the help() function comes from docstrings provided by the developers. While search results are often populated with the aforementioned forums, that information is not always correct, and even if it is, it may not be applicable to our version. By contrast, help() shows us the docstrings for our version, and so we can trust what it says.
  2. help() is always there for you – between smartphones, 5G, and broadband internet, it is easy to forget that sometimes the internet is not there for you! When there are major DNS issues, connectivity issues, or other problems that make the internet challenging to use as a resource, we can still fall back on help() to pull up documentation. Additionally, when working in secure facilities or working with industrial systems, internet connectivity is often hard to come by. In these cases, help() may be your only resource.

How To Use help()

One of the easiest ways to use help() is to simply run the command with no arguments. This will yield the interactive help system, where you can input a variety of terms to return information about modules, keywords, symbols, and other topics.

Using help() with modules will return a list of all the available modules and can allow us to get help with objects, functions, and other topics in the module. While many modules have excellent docstrings, some are a bit lacking – in these cases, help() can be a bit of a misnomer and we may have to rely on internet searches or, in the worst case scenario, trial and error. 

For those of us who are interested in contributing to open source projects or creating our own modules, help() is a major incentive to write docstrings as outlined in PEP 257 – an early Python Enhancement Proposal (PEP) that documents the semantics and conventions of Python docstrings.

However, especially for builtins and common Python data structures like list(), dict(), and tuple(), help() provides not only information about methods and subclasses for that object, but also provides valuable examples of how to create new instances of that class efficiently like in the built-in dict() class shown above.

What help() Won’t Do

While Python’s built-in help() can be a powerful tool for interactive use in the REPL, it is limited in a number of ways. 

  • Firstly, it can only be used interactively, so if you are working in an integrated development environment (IDE) or a text editor, it may be of limited use to you.
  • Secondly, it will not tell you anything that is not contained in docstrings, so for poorly documented modules or stand alone programs, it may not contain what you are looking for.

Additionally, while it may seem intuitive to add help for your own command-line program using the help() function, you’ll want to turn instead to a module like argparse to provide help to your users.

Conclusion

While Python’s built-in help() function does not see extensive use from programmers who have reliable access to the internet and may seem a bit anachronistic, it is still a tool worth keeping in your proverbial back pocket. Knowing how and where to use this function can be immensely beneficial to both newcomers and veterans alike. 

If you’ve mastered your built-in functions and are looking for something more data-centric, consider Udacity’s Programming for Data Science with Python Nanodegree.