ChecklistFor every function you are writing, consider the following suggestions to remove as many errors from your work as possible:
What makes a good documentation string?A good documentation string comments on (i) the parameters given to the function, (ii) the return value of the object and (iii) what the function does to get from the parameters to the return value. Additional information may include examples, type information, exceptions the function may raise, information about the algorithm used, etc. Here is a minimalistic example: def average(a, b): """Compute and return the arithmetic mean of inputs. Given parameters a and b, return the arithmetic mean.compute and return the arithmetic mean of a and b.""" return (a + b) * 0.5 A more detailed example: def mysum(a, b): """Return the sum of parameters a and b. Parameters ---------- a : numeric first input b : numeric second input Returns ------- a+b : numeric returns the sum (using the + operator) of a and b. The return type will depend on the types of `a` and `b`, and what the plus operator returns. Examples -------- >>> mysum(10, 20) 30 >>> mysum(1.5, -4) -2.5 Notes ----- History: example function first created 2002, last modified 2013 """ return a + b As a minimum guidance, the documentation string should explain what the parameters are (and use of the word parameters is encouraged) and what the function returns (and use of the word return is encouraged). Going beyond this, the documentation string needs to provide a reasonable summary of (i) what the function does and (ii) what restrictions there are on the valibity of results or values of parameters. One needs to use common sense here. You may want to use python’s help function or Spyder’s object explorer to see examples of documentation strings from in-built Python commands or library functions. For the short practice functions we write in this course, the documentation string may often seen unnecessary, but it is good practice to create at least a short documenation string that explains what the parameters and return values are. A more detailed discussion of documentation style conventions is available for the
See also how to structure docstrings so they look nice in Spyder You can instruct Spyder to highlight deviations from documentation string conventions: Preferences -> Completion and linting -> Docstring style -> [ ] Enable docstring linting (you can choose Numpy or PEP2 257 convention). Advanced suggestions
|
|