Home Backend Development Python Tutorial Python study notes - writing help documents for custom classes or functions, and conducting document testing

Python study notes - writing help documents for custom classes or functions, and conducting document testing

Jan 17, 2017 pm 02:27 PM
python Document testing

In python we can use help("module name") or help(class name) to view the documentation of a class or function. But how are they written? In fact, they use """ three double quotes to wrap multiple lines of comments at the beginning of the class or the method. These contents will be regarded as help documents by Python.


What content is generally written in the help document? It mainly includes the following content:


##The main function of this class or function


##Incoming values ​​and output values


Instructions for some special situations


Document test content


The above content is a personal summary, but I have not seen relevant information


Let’s give 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"
Copy after login

Enter

>>> 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)
Copy after login

Use doctest for document testing in the python terminal


We can also use the doctest module for document testing in the comments.

#For example, after we add the document test content, it looks like this:

class Apple(object):
"""
This is an Apple Class
Example:
>>> 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 = count
def get_count(self):
return self._count * self._countif __name__ == '__main__':
import doctest
Copy after login

doctest.testmod()

Because we wrote

if __name__ == '__main__':
import doctest
doctest.testmod()
Copy after login

So the above document test will only be performed when executing with the entry file, so it will not be executed in actual application.

## The above are the python study notes - writing help documents for custom classes or functions, and document testing. For more related content, please pay attention to the PHP Chinese website (www.php.cn)


#! ##

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to efficiently copy the entire column of one DataFrame into another DataFrame with different structures in Python? How to efficiently copy the entire column of one DataFrame into another DataFrame with different structures in Python? Apr 01, 2025 pm 11:15 PM

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

Can Python parameter annotations use strings? Can Python parameter annotations use strings? Apr 01, 2025 pm 08:39 PM

Alternative usage of Python parameter annotations In Python programming, parameter annotations are a very useful function that can help developers better understand and use functions...

How do Python scripts clear output to cursor position at a specific location? How do Python scripts clear output to cursor position at a specific location? Apr 01, 2025 pm 11:30 PM

How do Python scripts clear output to cursor position at a specific location? When writing Python scripts, it is common to clear the previous output to the cursor position...

Why can't my code get the data returned by the API? How to solve this problem? Why can't my code get the data returned by the API? How to solve this problem? Apr 01, 2025 pm 08:09 PM

Why can't my code get the data returned by the API? In programming, we often encounter the problem of returning null values ​​when API calls, which is not only confusing...

How does Uvicorn continuously listen for HTTP requests without serving_forever()? How does Uvicorn continuously listen for HTTP requests without serving_forever()? Apr 01, 2025 pm 10:51 PM

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

How to dynamically create an object through a string and call its methods in Python? How to dynamically create an object through a string and call its methods in Python? Apr 01, 2025 pm 11:18 PM

In Python, how to dynamically create an object through a string and call its methods? This is a common programming requirement, especially if it needs to be configured or run...

Python hourglass graph drawing: How to avoid variable undefined errors? Python hourglass graph drawing: How to avoid variable undefined errors? Apr 01, 2025 pm 06:27 PM

Getting started with Python: Hourglass Graphic Drawing and Input Verification This article will solve the variable definition problem encountered by a Python novice in the hourglass Graphic Drawing Program. Code...

Where to download Python .whl files under Windows? Where to download Python .whl files under Windows? Apr 01, 2025 pm 08:18 PM

Python binary library (.whl) download method explores the difficulties many Python developers encounter when installing certain libraries on Windows systems. A common solution...

See all articles