In Python we can use the Help ("module name") or Help (class name) to view the document of a class or function. But how are they written? In fact, they wrap a multiline comment in the "" "three double quotes at the front of the class or at the front of the method. This content will be treated as a help document by Python.
What does the help document usually say? The main contents include the following:
The main role of the class or function
Values passed in and output
Description of some special cases
Document Test Content
The above content is a personal summary, but did not see the relevant information.
Let's give an example:
Class Apple (object): "" "" "" "," "" "" "" "" Def Get_color (self): "" "Get the Color of Apple.get_color (self), str" " "Return" Red "
In Python terminal input
>>> from Calldemo import apple>>> Help (Apple) Help on class Apple in module Calldemo:class Apple (__builti N__.object) | This was 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)
Document Testing with Doctest
We can also doctest the module for documentation testing in our comments.
For example, we added the document test content as follows:
Class Apple (object): "" "" "The" "" "" "" "" is apple classexample:>>> Apple = apple () >>> apple.get_color () ' Red ' >>> apple.set_count >>> Apple.get_count () "" "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 ()
As we have written
if __name__ = = ' __main__ ': Import Doctestdoctest.testmod ()
As a result, the above documents are tested only when they are executed with a portal file. Therefore, it does not actually apply in the execution of the document test.
These are Python learning notes-writing help documents for custom classes or functions, as well as documentation testing, and more about topic.alibabacloud.com (www.php.cn)!