PinnIver
PinnIver

Reputation: 33

Create a help line for each function in module

I've made a couple of python files with functions like:

def func(a,b,c):
   return a+b+c

The functions are often more complicated, but the general idea of them all are the same, receive a couple of predetermined parameters, and return the desired value.

Now, I want to make a separate GUI file for easy use of all the functions. The GUI imports all the different python scripts and via text entry fields I want to be able to give the desired parameters for each function. The problem is that the functions vary greatly in what kinds of parameters they expect, so I wanted to make each function able to return a help string to the GUI with a short explanation of what the function needs to run. In the GUI file I'm using getattr to save the desired function:

methodtocall = getattr(Nuclear, func)

I can then call the function with parameters gained for textfields, like:

methodtocall(textfield1.get(),textfield2.get())

The problem, as I said above, is that for the GUI to be useful, I need a help sentence to be printed to the GUI the moment I select a function. I tried to make an adjustment in the functions themselves, example:

def func(a,b,c):
    help = "give a as string, b as list, c as integer"
    if no_parameters:
        return help
    else:
        return desired_value

The problem is that the methodtocall function gives a TypeError, since I try to launch it without any parameters: methodtocall()

The solution would be something that called the desired function the moment I select it in the GUI, and retrieves the help line for the desired function and displays it in a help textfield in the GUI, does anyone have any ideas?

Upvotes: 1

Views: 35

Answers (1)

DYZ
DYZ

Reputation: 57033

Place the help text in the first unassigned string in the function definition, like this:

def func(a,b,c): 
  """
  This function adds three numbers.
  """
  return a+b+c

Now, this text is available through the variable __doc__:

print(func.__doc__)
#
# This function adds three numbers.
# 

You do not even need to call the function.

Upvotes: 1

Related Questions