Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write help/description text for Python functions

Tags:

I recently started programming using Python. I have to write many functions and was wondering how I can incorporate a help or description text such that it appears in the object inspector of spyder when I call the function. In MatLab, this worked by putting commented text at the beginning of the function file. Is there a similar method in Python (using Spyder)?

like image 524
user1984653 Avatar asked Feb 21 '14 09:02

user1984653


People also ask

How do I add help to a Python function?

You have to use Ctrl + I in front of an object's name to show their help in our Help pane.

What is help () in Python?

Python help() Function. Python help() function is used to get help related to the object passed during the call. It takes an optional parameter and returns help information. If no argument is given, it shows the Python help console. It internally calls python's help function.

How do I get help from a Python module?

To start Python's interactive help, type "help()" at the prompt.

How do you write a comment or a docstring in a function?

It is written by using # symbol. Whereas Python Docstrings as mentioned above provides a convenient way of associating documentation with Python modules, functions, classes, and methods.


2 Answers

By default, the first string in the body of a method is used as its docstring (or documentation string). Python will use this when help() is called for that method.

def foo(bar):
    """
    Takes bar and does some things to it.
    """
    return bar

help(foo)
foo(bar)
    Takes bar and does
    some things to it

You can read more about how this works by reading PEP-258, and this question goes into some more details.

like image 86
Burhan Khalid Avatar answered Oct 10 '22 06:10

Burhan Khalid


(Spyder maintainer here) There are other couple of things you need to know (besides what @burhan-khalid mentioned) regarding Spyder itself:

  1. If you want to see your docstrings nicely formatted in the Help pane, you need to write them following the numpydoc standard, which is explained here. This is a set of conventions used by almost all python scientific packages to write their docstrings. It's not mandatory but we follow it too while converting docstrings (which come in plain text) to html.

  2. You have to use Ctrl+I in front of an object's name to show their help in our Help pane.

like image 23
Carlos Cordoba Avatar answered Oct 10 '22 06:10

Carlos Cordoba