⊗pyPmFnFDO 15 of 128 menu

Printing Function Documentation in Python

In the function body, you can write a line describing the essence of the work being done. Such a line is considered to be the documentation of the function. It is enclosed in single quotes and placed in the first line of the function body. To read it, you need to use the special function help, in the parameter of which the function name is passed.

Let's say we have a function that sums two numbers. A line with the relevant information is left about this:

def getSum(num1, num2): 'this function sums two numbers' return num1 + num2

Let's call help and find out what is commented out in getSum:

help(getSum)

After executing the code, a line with the function name and its documentation will appear in the console:

Help on function getSum in module __main__: getSum(num1, num2) this function sums two numbers

The help function can be used to read documentation for standard functions. Let's find out information about the print function:

help(print)

To output only the documentation string from a function, you need to pass the help service construct .__doc__ after the function name to help:

help(print.__doc__)

Create a function that will take a list of month names as a parameter and output them with a capital letter. Describe the essence of the function in the documentation and output it to the console.

Print all documentation about the function sum.

Print only the line with documentation about the function len.

enru