Python Learning notes-writing a help document for a custom class or function, and testing the document

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

  • Related Article

    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.