In python, we can use help (& quot; Module Name & quot;) or help (class name) to view the class or function documentation. But how did they be written? In fact, they enclose multiple lines of comments with the & quot; double quotation marks at the beginning of the class or method. This content is considered as a help document by Python. In python, we can use help ("module name") or help (class name) to view the class or function documentation. But how did they be written? In fact, they enclose multiple lines of comments in double quotes at the beginning of the class or at the beginning of the method. This content is considered as a help document by Python.
So what content does the help document generally write? It mainly includes the following content:
Main functions of this class or function
Input value and output value
Some special cases
Document test content
The above is my summary, but no relevant information is available.
Let's take an example:
class Apple(object):""" This is an Apple Class"""def get_color(self):"""Get the Color of Apple.get_color(self) -> str"""return "red"
Input in python terminal
>>> from CallDemo import Apple>>> help(Apple)Help on class Apple in module CallDemo:class Apple(__builtin__.object)| This is an Apple Class| | Methods defined here:| | get_color(self)| Get the Color of Apple.| get_color(self) -> str| | ----------------------------------------------------------------------| Data descriptors defined here:| | __dict__| dictionary for instance variables (if defined)| | __weakref__| list of weak references to the object (if defined)
Use doctest for document testing
In the annotations, we can also perform document testing in the doctest module.
For example, we add the document test content as follows:
class Apple(object):"""This is an Apple ClassExample:>>> apple = Apple()>>> apple.get_color()'red'>>> apple.set_count(20)>>> apple.get_count()400"""def get_color(self):"""Get the Color of Apple.get_color(self) -> str"""return "red"def set_count(self, count):self._count = countdef get_count(self):return self._count * self._countif __name__ == '__main__':import doctest
Doctest. testmod ()
Because we wrote
if __name__ == '__main__':import doctestdoctest.testmod()
Therefore, the above document test will be performed only when it is executed in the entry file. Therefore, document testing is not performed in actual applications.
The above is the python learning notes-help documentation for custom classes or functions and content for document testing. For more information, see The PHP Chinese website (www.php1.cn )!