Lecture 55: Python defines docstrings for functions

1. The concept of document strings

For functions, modules, classes, and methods, a document string is defined. The so-called document string is to define a help message, an explanation, and tell the user what the function is for. The document string is generally located in the function The first line in the body, usually enclosed in triple quotes.

It is called a docstring because tools can be used to print out the docstring of a function based on the docstring defined in the code.

As a qualified developer, you should develop the habit of writing document strings to improve the readability of the program.

Rules for defining docstrings in functions:

  • In the first line of the docstring, the theme is concise. When you see this docstring, you can understand the function and function of the function.
  • The first letter of the first line of a docstring is generally capitalized, and the first line ends with a period.
  • If the docstring contains multiple lines, the second line is usually a blank line, and the detailed description starts from the third line.

2. Definition of docstring

To view the docstring of a function, __doc__query through this property.

print(sorted.__doc__

Guess you like

Origin blog.csdn.net/weixin_44953658/article/details/130999947