Python entry coding style specification sharing:

This project is not an official Google project, but is created and maintained by domestic programmers with enthusiasm.

If you are following the official English version of Google, please go to Google Style Guide

In the following code, Yes means recommendation, and No means not recommended.

semicolon

Do not put a semicolon at the end of the line, and do not put two commands on the same line with a semicolon.

Line length

No more than 80 characters per line

Except for the following:

Long import module statement

URL in the comment

Do not use a backslash to connect lines.

Python implicitly concatenates the lines in parentheses, brackets, and curly braces. You can take advantage of this feature. If you want, you can add an extra pair of parentheses around the expression.

Recommended: foo_bar(self,width,height,color='black',design=None,x='foo',emphasis=None,highlight=0)if(width==0andheight==0andcolor=='red'andemphasis= ='strong'):

If a text string doesn't fit on one line, you can use parentheses to implement an implicit row join:

x=('This is a very long very long very long very long '' very long very long very long very long very long very long string ')

In the comment, place the long URL on one line if necessary.

Yes:# See details at#No:# See details at# \# v2.0/csv_file_name_extension_full_specificaTIon.html

Note the indentation of the elements in the example above; you can find an explanation in the :ref:`indentation section of this article.

brackets

Inadequate use of parentheses

Do not use parentheses in return statements or conditional statements unless they are used to implement row joins. However, it is possible to use parentheses around the tuple.

Yes:iffoo:bar()whilex:x=bar()ifxandy:bar()ifnotx:bar()returnfoofor(x,y)indict.items():...No:if(x):bar()ifnot (x):bar()return(foo) indent

Indent the code with 4 spaces

Never use tabs, nor do tabs and spaces. For line joins, you should either align the elements of the newline vertically (see the ref:`line length` section for an example), or use 4 spaces for hanging indents ( At this time, the first line should not have parameters):

Yes:# Align with the starting variable foo=long_funcTIon_name(var_one,var_two,var_three,var_four)# Align with the starting value in the dictionary foo={long_dicTIonary_key:value1+value2,...}# 4 spaces indent, first Line does not need foo=long_funcTIon_name(var_one,var_two,var_three,var_four)# 4 spaces in the dictionary are indented foo={long_dictionary_key:long_dictionary_value,...}No:# The first line has spaces that are forbidden foo=long_function_name(var_one , var_two, var_three, var_four) # 2 spaces are forbidden foo=long_function_name(var_one,var_two,var_three,var_four)# The dictionary does not handle indentation foo={long_dictionary_key:long_dictionary_value,...} blank line

Two lines between top-level definitions, one line between method definitions

There are two lines between the top-level definitions, such as function or class definition. The method definition, between the class definition and the first method, should be a blank line. In a function or method, some places are empty if you think it is appropriate.

Space

Use spaces on both sides of the punctuation according to standard typographic specifications

Do not have spaces in parentheses.

Use spaces on both sides of the punctuation according to standard typographic specifications

Yes: spam(ham[1],{eggs:2},[])No:spam(ham[1],{eggs:2},[])

Do not put spaces before the comma, semicolon, and colon, but they should be added after them (except at the end of the line).

Yes: ifx==4:printx,yx,y=y,xNo:ifx==4:printx,yx,y=y,x

There should be no spaces before the left parenthesis of the parameter list, index or slice.

Yes: spam(1)no:spam(1)Yes:dict['key']=list[index]No:dict['key']=list[index]

Add a space to both sides of the binary operator, such as assignment (=), compare (==, <, >, !=, <>, <=, >=, in, not in, is, is not), Boolean (and, or, not). As for how to use the spaces on both sides of the arithmetic operator, you need to make a good judgment. However, it is necessary to keep the two sides consistent.

Yes: x==1No: x<1

When '=' is used to indicate keyword parameters or default parameter values, do not use spaces on both sides.

Yes:defcomplex(real,imag=0.0):returnmagic(r=real,i=imag)No:defcomplex(real,imag=0.0):returnmagic(r=real,i=imag)

Do not use spaces to vertically align markers between multiple lines, as this can be a maintenance burden (applies to:, #, =, etc):

Yes:foo=1000# Comments long_name=2# Comments do not need to be aligned dictionary={"foo":1,"long_name":2,}No:foo=1000# Comments long_name=2# Comments do not need to be aligned dictionary={" Foo":1,"long_name":2,}Shebang

Most .py files don't have to start with #! as a file. According to PEP-394, the main file of the program should start with #!/usr/bin/python2 or #!/usr/bin/python3.

(Translator's Note: In computer science, Shebang (also known as Hashbang) is a string line (#!) consisting of a pound sign and an exclamation mark, which appears in the first two characters of the first line of the text file. In the case of Shebang, the program loader of the Unix-like operating system analyzes the contents of Shebang, uses the content as an interpreter instruction, and calls the instruction, and uses the file path containing Shebang as the argument of the interpreter. For example, a file starting with the directive #!/bin/sh will actually call the /bin/sh program when it is executed.)

#! is used first to help the kernel find the Python interpreter, but it will be ignored when importing the module. Therefore, it is only necessary to add #!.

Comment

Make sure to use the correct style for modules, functions, methods, and inline comments

Document string

Python has a unique way of commenting: Using a document string. A document string 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 are pydoc Used. (You can run pydoc on your module and see what it looks like.) Our convention for docstrings is to use triple double quotes """( PEP-257 ). A docstring should This organization: First is an outline of a line ending with a period, question mark or exclamation point (or the document string is only one line). Next is a blank line. Next is the rest of the document string, it should be the same as the docstring The first quote of a line is aligned. There are more formatting specifications for the document string below.

Module

Each file should contain a license template. Depending on the license used by the project (for example, Apache 2.0, BSD, LGPL, GPL), choose the appropriate template.

Functions and methods

The functions referred to below include functions, methods, and generators.

A function must have a docstring unless it satisfies the following conditions:

Externally invisible

Very short

easy to understand

The document string should contain a description of what the function does, as well as a detailed description of the input and output. Generally, you should not describe "how to do it" unless it is a complex algorithm. The document string should provide enough information to be called when someone writes the code. When he doesn't need to look at a single line of code, just look at the docstring. For complex code, it's more meaningful to add a comment next to the code than to use a docstring.

Several aspects of the function should be described in a specific section, as described below. Each section should start with a header line. The header line ends with a colon. In addition to the header line, the rest of the section should Indented by 2 spaces.

Args: Lists the name of each parameter, and uses a colon and a space after the name to separate the description of the parameter. If the description is too long to exceed a single line of 80 characters, use 2 or 4 spaces for the indentation (with The rest of the file is consistent.) The description should include the type and meaning required. If a function accepts *foo (variable length parameter list) or **bar (any keyword parameter), *foo and ** should be listed in detail. Bar. Returns: (or Yields: for the generator) Describes the type and semantics of the returned value. If the function returns None, this part can be omitted. Raises: Lists all exceptions related to the interface.
Deffetch_bigtable_rows(big_table,keys,other_silly_variable=None): """Fetches rows from a Bigtable. Retrieves rows contributing to the given keys from the Table instance represented by big_table. Silly things may happen if other_silly_variable is not None. Args: big_table: An Keys: A sequence of strings representing the key of each table row to fetch. other_silly_variable: Another optional variable, that has a much longer name than the other args, and which does nothing. Returns: A dict mapping keys to The corresponding table row data fetched. Each row is represented as a tuple of strings. For example: {'Serak': ('Rigel VII', 'Preparer'), 'Zim': ('Irk', 'Invader'), 'Lrrr': ('Omicron Persei 8', 'Emperor')} If a key from the keys argument is missing from the dictionary, then that row was not found in the table. Raises: IOError: An error occurred accessing the bigtable. Table object. """pass

class

A class should have a document string under its definition that describes the class. If your class has public attributes (Attributes), then there should be an attribute (Attributes) section in the document. It should follow the same format as the function parameters. .

classSampleClass(object):"""Summary of class here. Longer class information.... Longer class information.... Attributes: likes_spam: A boolean indicating if we like SPAM or not. eggs: An integer count of the eggs we Have ""def__init__(self,likes_spam=False):"""Inits SampleClass with blah."""self.likes_spam=likes_spamself.eggs=0defpublic_method(self):"""Performs operation blah."""

Block comments and line comments

The most important part of the code is the technical part of the code. If you have to explain it in the next code review, you should write it now. For complex operations, you should write a few before the start of the operation. Line comments. For code that is not at a glance, you should add a comment at the end of its line.

# We use a weighted dictionary search to find out where i is in# the array. We extrapolate position based on the largest num# in the array and the array size and then do binary search to# get the exact number.ifi&(i- 1)==0:# true iff i is a power of 2

For readability, the comment should be at least 2 spaces away from the code.

Prefilled Vape

China manufacture for Popular Custom Vape Pen, high quality battery, quality e-liquid and food safty material.

Prefilled Vape

Newmax Electronics Co.,Limited , https://www.advvape.com