I am trying to make my own module and I want to show the users a help for the module functions

Viewed 17

I am creating a module in which I'm defining multiple functions and I want to give the users some help regarding it, just like the help() function does. Still, I'm not quite sure how I do that, so can someone help me with it, for example, if I want help regarding this function, what will I have to do?

def shanky_calculate_average(*args):
    my_average  = sum(args) / len(args)
    return my_average

I just want to know if I can get help in the same way as you can on things like help(pandas.read_excel)

1 Answers

What help() actually does is printing the function name with arguments and its docstring. Docstrings are comments directly after the function or class definition and are declared using either '''triple single quotes''' or """triple double quotes""".

So doing something like this:

def shanky_calculate_average(*args):
    '''
       Computes the average of the arguments.
       Example: shanky_calculate_average(1,2,3,4)
    '''
    my_average  = sum(args) / len(args)
    return my_average

will result in help(shanky_calculate_average) to print

shanky_calculate_average(*args)
    Computes the average of the arguments.
    Example: shanky_calculate_average(1,2,3,4)

As a sidenote, you can also access docstrings via the __doc__ property of the object like this: shanky_calculate_average.__doc__.

Related