Machine Learning Algorithms and Python Learn Simple Code Conventions

0, foreword

This article is the most refined and simple coding specification summarized after reading "Python Coding Rule". According to each person's different preferences, there will be different choices in some places. I just made the most simple and easy choice for myself, only for everyone. reference.

1, the important principle

a. Keeping style consistency is important, but the most important one is: Knowing when to be inconsistent b. Two good reasons to break a given rule: c. When the rules are applied will cause code readability to decrease (readability high d. Break the rules in order to be consistent with the surrounding code (historical legacy)

2, the most simple specification

a. Use only space for indentation b. Use UTF-8 encoding c. Write only one statement per line d. Use line-ending backslashes to collapse long lines, limiting each line to a maximum of 79 characters e. Importing packages: Unique, large from each line To a small, absolute path f. Intraclass methods are separated by 1 line. The outer space is separated by 2 lines. The g. operator is divided by *, and the two sides are separated by 1 space. Function parameters are not surrounded by spaces. h. Use the hump method other than the class name. , Other modules, functions, methods, variables use all lowercase + underscores i. 1 leading underscore indicates semi-publishing, 2 leading underscores indicate private, and reserved words use a single post-underscore j. development uses Chinese notes, publish Write English documents again

3, detailed specifications

a. Full-text generic b. Use only space for indentation, 4 spaces for 1 indentation level c. Each line is limited to 79 characters, use line-end backslash to collapse long lines d. Use UTF-8 encoding e. Line only write one statement

4, code naming

One line imports only one package. The order of Imports is: standard library, related main package, specific application, one blank line between each set of import, and all imports use the absolute path of the package.

The definition of splitting top-level functions and classes uses two blank lines. The split-class method definition uses one blank line. There must be one blank line between the class line and the first method definition.

Use spaces in English as a whole, that is, only add a space after the comma and semicolon. Any other symbols such as parentheses, square brackets, curly braces, etc. do not use spaces to separate symbols from characters. Write them together to indicate a whole. Operators are separated by a space except for the * symbol; other symbols are separated by a space; there is no space around the function parameter = number.

Module name: no underscore, short, all lower case;

Class name, anomaly name: The hump method of initial capital string;

Function names, global variable names, method names, instance variables: all lowercase, underlined to increase readability;

A leading underscore only applies to internal methods and instance variables that are not intended to be public interfaces of the class for global variables that are not to be imported (as well as internal functions and classes);

The two leading underscores are used to indicate the private name of the class and are only used to avoid name conflicts with the attributes in the class (designed for subclassing).

Private attributes must have two leading underscores, no post underscores;

Non-public properties must have a leading underscore without a post underscore.

Public attributes have no leading and trailing underscores, unless they conflict with reserved words. In this case, a single post-underscore is better than pre- or chaotic spelling, for example: class_ is better than klass.

5, writing skills

Compare with single values ​​like None, always use: 'is' or 'is not': if x is not None

Define base exception classes in modules and packages

Use string methods instead of string modules.

Avoid string slicing when checking prefixes or suffixes, use startswith() and endswith() instead, eg: No: if foo[:3] == 'bar':Yes: if foo.startswith('bar') :

Use only isinstance() for object type comparisons, such as: No: if type(obj) is type(1): Yes: if isinstance(obj, int)

Do not use True = True or False = =, such as: No: if greeting == True: Yes: if greeting:

6. Notes

When developing, the comments are all written in Chinese, and when the script tool is to be published, the English document is written again.

The comment should be a complete sentence (a phrase can also be), with the first letter capitalized; if the comment is short, the trailing period is omitted; the comment block is composed of one or more complete sentences, and each sentence ends with a sentence; the end of the sentence Use two spaces after the period.

The comment block starts with # and a space in each line, and the code following the comment has the same indentation level. There is an empty line above and below the comment block.

Use inline comments with caution, using at least two spaces separate from statements.

Use pydoc; epydoc; Doxgen and other documentation tools to write docstrings for all common modules, functions, classes, and methods. docstrings are not necessary for non-public methods, but you should have a description of what this method does Note that this comment should be after the "def" line.

The """ at the end of a multi-line document string should stand alone.

Version annotation: Define a variable __version__ = "$Revision: 1.4 $"

Stay hungry. Stay foolish.

3000 Puffs Vape

3000 Puffs Vape,Breeze Vape Pod,Disposable Oil Vape Pen,Electronic Cigarette Aim Stick

Guangzhou Yunge Tianhong Electronic Technology Co., Ltd , https://www.e-cigaretteyfactory.com