


Python study notes - writing help documents for custom classes or functions, and conducting 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:
##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"
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)
Use doctest for document testing in the python terminal
We can also use the doctest module for document testing in the comments.
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
doctest.testmod()Because we wrote
if __name__ == '__main__': import doctest doctest.testmod()
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)
#! ##

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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...

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? 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? 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? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

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...

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...

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