[Python concise way]-----1. Comment rules

every blog every motto: You will never know unless you try

0. Preface

Digression: When I first learned python, the teacher also taught some code specifications, and I didn't care about writing code at first. After a while, I will look at my own code again, my heart is... (my God, what kind of shit is this,...), and I look at the code written by others, my heart..., slowly realize the importance of standard code Sex. The comment is the basis of the code specification.

whatever, from now on, work hard together. Reject shit, embrace elegant code! , Your code can be very elegant! ! !

Note: This series of columns mainly explain related python specifications. The idea has been around for a long time, and I haven’t gotten hands on it. Let’s start the first one-comment rules

1. Text

1.1 Line comment

A space after "#" can be written above or after the sentence. Generally, the sentence (or comment) is shorter, and the comment is written on the right side of the sentence; the sentence (or comment) is longer, and the comment is written above the sentence. The details are shown in the following figure:
Explanation: When the comment is written at the back of the sentence, there are two spaces between "#" and the code! !

if array_channel == 0:  # 如果是第一个通道,要新建数组(5888,5888,10)
	H = arr.shape[0]  # 图像的长 5888
	W = arr.shape[1]  # 图像的宽 5888
# 加载数据,返回投影,几何信息,图片数组(5888,5888,10)
projection, transform, img_arry = self.load_img_to_array(img_input_path)

1.2 Block comments

Each line starts with "#", as shown below:

# 块注释
# 块注释
#
# 块注释
# 块注释
# load all relevent bands of a image file
# input:
        imgFile: image file
# output:
#         prj: projection data
#         trans: transform data
#         matImg: matrix containing the bands of the image

1.3 Function (document) comment (Docstring)

When the program is written for a long time, it is very troublesome to jump back and forth between functions to view the specific information of the function. Add comments to the specification can quickly view the information of the function.

  • Docstring as a document generally appears in the head of the module, function, and class, so that Python can get the document through the doc object of the object
  • Editors and IDEs can also give automatic prompts based on Docstring
  • Documentation comments begin and end with """ (three double quotes).
  • After the function definition is complete, write a comment
    tip: After the function definition is complete, enter three pairs of double quotation marks in the first line of the function to automatically generate a fixed-format comment, and just fill in the necessary information.

1.3.1 Specific steps

Step 1: Enter three pairs of double quotation marks
. The red line below is where the cursor is.
Insert picture description here
Step 2: Press Enter to
automatically generate a fixed format. Just fill in the necessary information.
Insert picture description here
Step 3: Fill in the information The
Insert picture description here
final code is as follows.

def load_img_to_array(self, img_file_path):
    """
     读取栅格数据,将其转换成对应数组
     :param: img_file_path: 栅格数据路径
     :return: 返回投影,几何信息,和转换后的数组(5888,5888,10)
     """
     print('这是代码句')
     print('这是代码句')
     print('这是代码句')

1.3.2 Quickly view comments (the benefits of standardized comment code)

Place the cursor on the name of the function called by the function and press Ctrl + Q to quickly display the information of the function, as shown in the figure below:
This way, you don’t have to go to the function definition to see what the function is doing. Is it very convenient? , Is there any! ! ! !
Insert picture description here

1.3.3 Going further (added on 2020.10.13 11:32)

The more standardized one should be to leave a line between the description and the parameters , so that the code is more standardized (the parameters will not be mixed in the description, as shown in the figure above), as shown in the figure below:
Description: pycharm2020.2.3 (the latest version) is more convenient and silky ,Recommended Use!
Insert picture description here

1.4 Paragraph Notes

Note: This is my personal experience, unlike the previous ones, which are all made by convention, please consider it.
When a function definition is long, the function can be divided into several parts. You can use three pairs of single quotes to annotate each part.
Attachment: In order to facilitate display, some of the codes below have been folded.
Insert picture description here

1.5 Important code comments

In important code or more complex places, generally use multiple "=" to separate, can be more eye-catching. The details are as follows:

app = create_app(name, options)


# =====================================
# 请勿在此处添加 get post等app路由行为 !!!
# =====================================


if __name__ == '__main__':
    app.run()

1.6 Quick Specification Code

In Pycharm, you can use Ctrl + Alt + L to quickly standardize the code to make it conform to the PEP8 standard. Of course, it only adjusts blank lines and spaces, and does not help you add comments.

references

[1] https://www.jianshu.com/p/5ea95e6cf8e4

Guess you like

Origin blog.csdn.net/weixin_39190382/article/details/106498812