Tencent's Python coding standards

Copyright notice: This article is for the CSDN blogger "Tianyuan Prodigal Son"
Original link: https://blog.csdn.net/xufive/article/details/84957425

The composition of the python file

In order to facilitate the description, first a demo

#! /usr/bin/env python
# - *- coding: utf-8-*-"""Usually this is the description (docstring) of this document, which must end with a period, question mark or exclamation mark!

There should be a blank line before this line, continue to complete the description of this document
If the document description can end in one line, the three double quotes at the end do not need to wrap; otherwise, it should be like the following
"""

import os, time
import datetime
import math

import numpy as np
import xlrd, xlwt, xlutils

import youth_mongodb
import youth_curl

BASE_PATH = r"d:\YouthGit"
LOG_FILE = u"Run log.txt"classGameRoom(object):"""Game room"""

 def __init__(self, name, limit=100,**kwds):"""Constructor!

  name Name of the game room
  limit
  kwds parameter dictionary
        """

  pass

def craete_and_start():"""Create and start the game room"""

 pass

if __name__ =='__main__':
 # Open game service
 start()

On the Linux platform, a python source code file should consist of the following parts. On the Windows platform, the first item can be omitted.

  1. Interpreter declaration
  2. Encoding format declaration
  3. Module comment or docstring
  4. Module import
  5. Constant and global variable declaration
  6. Top-level definition (function or class definition)
  7. Execute code

Encoding format declaration

Usually, the encoding format declaration is required. If the python source code file does not declare the encoding format, the python interpreter will use ASCII encoding by default. Once the source code file contains non-ASCII characters, the python interpreter will report an error. Taking UTF-8 as an example, the following two encoding format declarations are in compliance with the rules.

# - *- coding: utf-8-*-
# coding = utf-8

I always use UTF-8 encoding format, and like to use the first declaration method.

On the Windows platform, the encoding format declaration must be on the first line of the python file. On Linux platforms, the encoding format declaration is usually located in the second line of the python file, and the first line is the path declaration of the python interpreter.

#! /usr/bin/env python
# - *- coding: utf-8-*-

Indentation

Use 4 spaces for indentation. Never use tabs, and do not mix tabs and spaces. For line concatenation, I generally use a hanging indent of 4 spaces. E.g:

var_dict ={'name':'xufive','mail':'[email protected]'}

Quotation marks

Comment

There is a space after the number, and the paragraphs are separated by blank lines (# is also required):

 # Block comment
 # Block comment
    #
 # Block comment
 # Block comment

In-line comments, use at least two spaces and statements to separate:

age +=1  #One year older

More important comment paragraphs, separated by multiple equal signs, can be more eye-catching and highlight the importance:

 server=gogame(room, options)

    # =====================================
 # Do not dump garbage here!!!
 # ===================================== if __name__ =='__main__':
  server.run()

Blank line

Space

Docstring

The docstring is the first statement in a package, module, class, or function. These strings can be automatically extracted by the doc member of the object and used by pydoc. Use triple double quotation marks (""") for docstrings. If the content of the docstring cannot be written on one line, the first line must end with a period, question mark or exclamation mark, followed by a blank line, and the ending triple double quotation mark must be on its own line.

Import module

Imports should always be placed at the top of the file, after module comments and docstrings, and before module global variables and constants. Imports should be grouped in order from most common to least common, with a blank line between the groups:

  1. Standard library import
  2. Third-party library import
  3. Application specific import

The following import methods should be avoided:

from math import*

Naming conventions

Recommended reading:
[Heavy Sharing] White Paper on the Guidelines for Building a Recommendation System from Zero to One.pdf (with 48 pages download link)
[Heavyweight] Architecture evolution of tens of millions of intelligent recommendation systems
Head of Xiaohongshu Architecture: Application of Flink in Recommendation System, 25 ppt
106 Chinese version of "Advanced Python" page (download attached)
19 How to make the most popular music application for a year-old college student after learning Python

Have you learned after reading this article? Please forward and share to more people

Recommended Posts

Tencent's Python coding standards