I'm currently working with a pre-existing class (not written by me) that has functions which call other functions, passing through the parameters to the helper functions. Sometimes there are layers to these functions, so that func1
calls func2
calls func3
calls func4
, where a parameter of func4
is passed through all the way from func1
. In this case, it would not make sense to make any of these parameters instance/class attributes.
A 1-layer example:
def my_func(A, B, C, D):
# Calculate E with my_func args
E = some_calculations(A, B)
# Call helper, passing through some parameters
return helper(C=C, D=D, E=E)
def helper(c, d, e):
return some_other_calculations(C, D, E)
I'm trying to write some dosctrings for these functions, and I am having trouble formulating a way to effecitvely document the arguments that are passed through to the helper function(s).
In particular, I see a few options:
Method 1
Point to helper
's dosctring:
def my_func(A, B, C, D):
"""
Do some stuff
:param A: Parameter A description
:param B: Parameter B description
:param C: See `helper`
:param D: See `helper`
"""
...
return helper(...)
def helper(C, D, E)
"""
Do some other stuff
:param C: Parameter C description
:param D: Parameter D description
:param E: Parameter E description
"""
...
return some_other_calculations(...)
PRO: Only ever have to update/maintain one description of a given parameter
CON: If we add another function outer_func
that calls my_func
and passes C
and D
to it, which in turn passes C
and D
to helper, we will have to point to either my_func
or helper
.
In the former case this would lead to another forwarding, daisy-chaining the reader through multiple dosctrings just to get a description. In the latter case, it would be unclear where in outer_func
that helper
is called.
Method 2
Simply copy the description for each function that uses it
def my_func(A, B, C, D):
"""
Do some stuff
:param A: Parameter A description
:param B: Parameter B description
:param C: Parameter C description
:param D: Parameter D description
"""
...
return helper(...)
def helper(C, D, E)
"""
Do some other stuff
:param C: Parameter C description
:param D: Parameter D description
:param E: Parameter E description
"""
...
return some_other_calculations(...)
PRO: Do not have to look through other functions to understand the argument
CON: Obviously, multiple descriptions will have to be maintained. Also, it may difficult to describe the parameters without reference to the functions which use them, in which case you are probably forwarding them to the other functions anyway. This effectively leads to the cons of method 1 without the pro.
Method 3
Brief description + forwarding
def my_func(A, B, C, D):
"""
Do some stuff
:param A: Parameter A description
:param B: Parameter B description
:param C: Brief parameter C description. See `helper` for more info.
:param D: Brief parameter D description. See `helper` for more info.
"""
...
return helper(...)
def helper(C, D, E)
"""
Do some other stuff
:param C: Parameter C description
:param D: Parameter D description
:param E: Parameter E description
"""
...
return some_other_calculations(...)
The pros/cons here are kind of a blend of the above two methods
Method 4
Something else?
Any advice is greatly appreciated!