32

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)?

0

3 Answers 3

67

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.

Sign up to request clarification or add additional context in comments.

1 Comment

how to do it for packages?
12

(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.

1 Comment

I've found this very helpful, thanks! Is this the new place for that? numpydoc.readthedocs.io/en/latest/…
7

In a short answer. This can be done by putting text between triple quotes.

'''
@param self
'''

You can find a brief example on this link: https://www.jetbrains.com/help/pycharm/creating-documentation-comments.html#

The other answers are more extensive.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.