Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

In Colaboratory, you can write docstrings for functions quickly using the triple quotes (""") directly under the function's definition. After writing the opening triple quote, press the "Tab" key to auto-generate the docstring template. You can then fill in the required information about the function's purpose, parameters, and return value. Here's an example:

def add_numbers(num1, num2):
  """Adds two numbers together.

  Args:
    num1 (int): The first number to be added.
    num2 (int): The second number to be added.

  Returns:
    int: The sum of num1 and num2.
  """

  return num1 + num2

To see the docstring for a function, you can type the function's name followed by a question mark and run the cell. This will show the docstring in the output area. For example:

add_numbers?

This will display:

Signature: add_numbers(num1, num2)
Docstring: Adds two numbers together.

  Args:
    num1 (int): The first number to be added.
    num2 (int): The second number to be added.

  Returns:
    int: The sum of num1 and num2.
File:      /content/examples.py
Type:      function