Python learning notes-compile help documents for custom classes or functions and perform document testing

Source: Internet
Author: User
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 )!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.