Python Help() Function - DigitalOcean

  • Blog
  • Docs
  • Get Support
  • Contact Sales
  • Tutorials
  • Questions
  • Product Docs
  • Cloud Chats
  • Search Community

Report this

What is the reason for this report?This undefined is spamThis undefined is offensiveThis undefined is off-topicThis undefined is otherSubmit

Table of contents

  1. Python help function
  2. Defining help for custom class and functions
  3. Summary
  1. Tutorials
  2. Python
  3. Python help() function
TutorialPython help() functionPublished on August 3, 2022PythonPankaj Kumar

By Pankaj Kumar

Python help() functionTable of contentsPopular topics

Python help() function is used to get the documentation of specified module, class, function, variables etc. This method is generally used with python interpreter console to get details about python objects.

Python help() function

Python help() function syntax is:

help([object])

If no argument is given, the interactive help system starts on the interpreter console. python help utility console In python help console, we can specify module, class, function names to get their help documentation. Some of them are:

help> True help> collections help> builtins help> modules help> keywords help> symbols help> topics help> LOOPING

If you want to get out of help console, type quit. We can also get the help documentation directly from the python console by passing a parameter to help() function.

>>> help('collections') >>> help(print) >>> help(globals)

Let’s see what is the output of help() function for globals() function.

>>> help('builtins.globals') Help on built-in function globals in builtins: builtins.globals = globals() Return the dictionary containing the current scope's global variables. NOTE: Updates to this dictionary *will* affect name lookups in the current global scope and vice-versa.

Defining help() for custom class and functions

We can define help() function output for our custom classes and functions by defining docstring (documentation string). By default, the first comment string in the body of a method is used as its docstring. It’s surrounded by three double quotes. Let’s say we have a python file python_help_examples.py with following code.

def add(x, y): """ This function adds the given integer arguments :param x: integer :param y: integer :return: integer """ return x + y class Employee: """ Employee class, mapped to "employee" table in Database """ id = 0 name = '' def __init__(self, i, n): """ Employee object constructor :param i: integer, must be positive :param n: string """ self.id = i self.name = n

Notice that we have defined docstring for function, class and its methods. You should follow some format for documentation, I have generated some part of them automatically using PyCharm IDE. NumPy docstring guide is a good place to get some idea around proper way of help documentation. Let’s see how to get this docstring as help documentation in python console. First of all, we will have to execute this script in the console to load our function and class definition. We can do this using exec() command.

>>> exec(open("python_help_examples.py").read())

We can verify that the functions and class definitions are present using globals() command.

>>> globals() {'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <class '_frozen_importlib.BuiltinImporter'>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__warningregistry__': {'version': 0}, 'add': <function add at 0x100dda1e0>, 'Employee': <class '__main__.Employee'>}

Notice that ‘Employee’ and ‘add’ are present in the global scope dictionary. Now we can get the help documentation using help() function. Let’s look at some of the examples.

>>> help('python_help_examples')

python help custom module

>>> help('python_help_examples.add') Help on function add in python_help_examples: python_help_examples.add = add(x, y) This function adds the given integer arguments :param x: integer :param y: integer :return: integer (END)

python help function

>>> help('python_help_examples.Employee')

python help class

>>> help('python_help_examples.Employee.__init__') Help on function __init__ in python_help_examples.Employee: python_help_examples.Employee.__init__ = __init__(self, i, n) Employee object constructor :param i: integer, must be positive :param n: string (END)

python help class method

Summary

Python help() function is very helpful to get the details about modules, classes, and functions. It’s always best practice to define docstring for the custom classes and functions to explain their usage.

You can checkout complete python script and more Python examples from our GitHub Repository.

Reference: Official Documentation

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the author

Pankaj KumarPankaj KumarAuthorSee author profile

Java and Python Developer for 20+ years, Open Source Enthusiast, Founder of https://www.askpython.com/, https://www.linuxfordevices.com/, and JournalDev.com (acquired by DigitalOcean). Passionate about writing technical articles and sharing knowledge with others. Love Java, Python, Unix and related technologies. Follow my X @PankajWebDev

See author profileCategory:TutorialTags:PythonWhile we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Still looking for an answer?

Ask a questionSearch for more helpWas this helpful?YesNoComments(3)Follow-up questions(0)JournalDevJournalDevDigitalOcean Employee badgeApril 24, 2020Show less

HI Pankaj. I need some help.My interpreter does not want to execute my function. It says ‘keyboardinterrupt’ and disappears. What could be the problem and how fix it? Jerry

- Jerry Mokwena

JournalDevJournalDevDigitalOcean Employee badgeMay 13, 2020Show less

Would it be possible to send the help () result to a Text () text box ??? I have tried putting the help () result in a variable but it always gives NONE. Thank you

- Jose

JournalDevJournalDevDigitalOcean Employee badgeJune 21, 2020Show less

mdo_import_help Hello, Mr. Pankaj, I’m a new user, also a python newbie, hoping to learn here from your wisdom and experience. Now, are you familiar with mdo_import_help ? I’m trying to run some python code code but it keeps failing because mdo_import_ help is missing. Thanks very much, Mirabelle la Terreur

- Mirabelle1998

Creative CommonsThis work is licensed under a Creative Commons Attribution-NonCommercial- ShareAlike 4.0 International License.

Deploy on DigitalOcean

Click below to sign up for DigitalOcean's virtual machines, Databases, and AIML products.Sign up

Popular Topics

  1. AI/ML
  2. Ubuntu
  3. Linux Basics
  4. JavaScript
  5. Python
  6. MySQL
  7. Docker
  8. Kubernetes
  9. All tutorials
  10. Talk to an expert

Featured tutorials

  1. SOLID Design Principles Explained: Building Better Software Architecture
  2. How To Remove Docker Images, Containers, and Volumes
  3. How to Create a MySQL User and Grant Privileges (Step-by-Step)
  • All tutorials
  • All topic tags
Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Sign Up

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Learn more

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Learn more

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

SubmitSubmit

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

View all products

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

Get started

*This promotional offer applies to new accounts only.

© 2025 DigitalOcean, LLC.Sitemap.

Tag » How To Exit Python In Terminal