preface

Python learning journey, first look at the Python code specification, so that you have a sense of the first, and in the future learning slowly develop a habit

directory

A brief overview

1, coding

2. In the absence of special circumstances, #–coding: UTF-8 — must be added to the file header

2. Code format

2.1, indentation

Use four Spaces for indentation

2.2, line width

Each line of code should be no more than 80 characters (slightly more than 80 in special cases, but no more than 120)

Reason:

1. This is very helpful when looking at side-by-side diff 2. 3. Too long may be a design flaw

2.3, quotes,

In short, natural languages use double quotes and machine tags use single quotes, so most of your code should use single quotes

Natural language uses double quotation marks “…” Such as error messages; Many cases are still Unicode, using u” Hello world “machine identifiers using single quotes ‘… ‘For example, dict key regular expressions use the native double quotation marks r”…” The docString (docString) uses three double quotation marks “””……” “”

2.4, a blank line

Empty two lines between a module-level function and a class definition; An empty line between class member functions;

You can use blank lines to separate groups of related functions you can use blank lines to separate logically related code from functions

2.5, coding

File using UTF-8 encoding file header with #–conding: UTF-8 — logo

3. Import statements

Import statements should be written in line

The import statement should use absolute import

Import statements should be placed in the header of the file, after module description and docString, and before global variables. Import statements should be arranged in order, with each group separated by an empty line

You can use relative imports when importing class definitions for other modules

If a naming conflict occurs, namespaces can be used

4, white space

[=,-,+=,==,>,in,is not, and]:

In the argument list of the function, the comma must be followed by a space

In the argument list of the function, do not add Spaces around the default value equal sign

Do not add extra space after the open parenthesis and before the close parenthesis

Do not precede the opening parenthesis of a dictionary object with extra space

Do not use extra Spaces to align assignment statements

5, a newline

Python supports line breaks within parentheses. There are two situations.

1) The second line is indented to the beginning of the parentheses

2) Indent the second line with 4 Spaces. This applies when the opening parenthesis is a line break

With a backslash break, the binary operator +. Etc should appear at the end of the line; Long strings can also be wrapped this way

Disallow compound statements, that is, multiple statements in a single line:

If /for/while must be wrapped:

6, docstrings

The specification of docString has two most basic points:

1. All public modules, functions, classes, and methods should write docString. Private methods are not necessarily required, but should be specified with a block comment after def.

2. The end of a docString “”” should have a single line, unless the docString has only one line.

Second, the annotation

1, comments,

1.1. Block comments

One space after the “#” sign, paragraphs are separated by a blank line (the “#” sign is also required)

1.2. Line comments

Use at least two Spaces to separate statements, and be careful not to use meaningless comments

1.3, advice,

In the key part of the code (or more complex place), can write comments should try to write comments more important section, use multiple equal signs separated, can be more eye-catching, highlight the importance

2. Document annotation (Docstring)

Docstrings as documents are typically found in module headers, functions, and class headers, so that documents can be retrieved from the doc object of an object in Python. Editors and ides can also give automatic prompts based on Docstring.

Document comments start and end with “”. The first line is not newline. If there are more than one line, the last line must be newline

Do not copy the prototype of the function definition in the documentation comments, but describe it in detail, explaining the specific parameters, return values, and so on

Function parameters, return values, etc. are described using numPY standards, as shown below

Document comments are not limited to Chinese and English, but do not mix Chinese and English document comments are not the longer the better, usually one or two sentences can make the situation clear can module, public class, public method, can write document comments, should try to write document comments

3. Naming conventions

1, modules,

Use lowercase names for modules, keep the first letter in lower case, and avoid underscores (unless there are several words and not many).

2, the name of the class

Class names use the CamelCase style, with a capital letter, and private classes may begin with an underscore

Put related classes and top-level functions in the same module. Unlike Java, there is no need to limit each class to one module.

3, functions,

All function names are lowercase. If there are multiple words, separate them with underscores

Private functions are preceded by an underscore _

4. Variable names

The variable name should be lowercase. If there are multiple words, separate them with underscores (_)

Constants are all uppercase. If there are multiple words, separate them with underscores

5, constant

Constants are named in uppercase separated by an underscore

Python information is available in a private message