Google and pythongoogle

Source: Internet
Author: User

Google and pythongoogle

Mark the website at the beginning: goole Official Website

Programmers in any language compile code that complies with the specifications, which is the first step in their programming career.

I. semicolon
Do not add points at the end of a line, or use semicolons to place the two commands on the same line.
2. Line Length
Each line cannot exceed 80 characters

Exceptions:

  1. Long import module statement
  2. URL in the comment

Do not use a backslash to connect rows.

Python implicitly concatenates parentheses, braces, and rows in curly brackets. You can use this feature. If necessary, you can add an additional pair of parentheses to the outer side of the expression.

Yes: foo_bar(self, width, height, color='black', design=None, x='foo',             emphasis=None, highlight=0)     if (width == 0 and height == 0 and         color == 'red' and emphasis == 'strong'):

If a text string cannot be placed in one row, you can use parentheses to implement implicit row join.

x = ('This will build a very long long '     'long long long long long long string')

If necessary, place the long URL on a line.

Yes: # See details at # http://www.example.com/us/developer/documentation/api/content/v2.0/csv_file_name_extension_full_specification.html

No:  # See details at     # http://www.example.com/us/developer/documentation/api/content/\     # v2.0/csv_file_name_extension_full_specification.html
Brackets
We prefer to use brackets without abuse.

Do not use parentheses in the return statement or condition statement unless it is used to implement row join. However, it is acceptable to use parentheses on both sides of the tuples.

Iv. indent
Use four spaces to indent the code

Do not use tab, or mix tab and space.

For row join, you should either vertically align the elements of the line feed (see the example of the line length Section), or use a 4-space hanging indent (at this time, the first line should not have a parameter):

5. Empty rows
Two rows are empty between top-level definitions, and one row is empty between method definitions.
  • There are two blank rows between top-level definitions, such as functions or class definitions.
  • Method definition. A row should be blank between the class definition and the first method. In a function or method, if you think it is appropriate, leave a row empty.
6. Space
Use spaces on both sides of punctuation according to standard typographical specifications
7. Shebang
Most. py files do not need to use #! Start as a file. According to PEP-394, the main file of the program should start #! /Usr/bin/python2 or #! Start with/usr/bin/python3.

(Note: in computer science, Shebang (also known as Hashbang) is a string line consisting of a well number and an exclamation point (#!), It appears in the first two characters of the first line of the text file. when Shebang exists in the file, the program loader of the Unix-like operating system will analyze the Shebang content, use the content as the interpreter instruction, and call the instruction, the file path containing Shebang is used as the parameter of the interpreter. for example, the command #! Files starting with/bin/sh will actually call the/bin/sh program during execution .)

#! It is first used to help the kernel find the Python interpreter, but will be ignored when the module is imported. Therefore, it is necessary to add # To the file that is directly executed #!.

8. Notes
Make sure that the correct style is used for module, function, method, and in-line comments.

Block comment and line comment

The most important thing to note is the technical part of the code. if you need to explain it during the next code review, you should write comments to it now. for complex operations, you should write several lines of comments before the operation starts. for codes that are not clear at a glance, add comments at the end of the line.

To improve readability, comments should leave at least two spaces in the code.

On the other hand, never describe the Code. If the person who reads the code knows Python better than you, he just doesn't know what your code is going to do.

IX. Category
If a class does not inherit from other classes, it will explicitly inherit from the object. The same is true for nested classes.

Inherited fromobjectThis is to make properties work normally and protect your code from a special potential incompatibility of PEP-3000. this also defines some special methods that implement the default semantics of objects, including

__new__, __init__, __delattr__, __getattribute__, __setattr__, __hash__, __repr__, and __str__
10. String
Even if the parameters are strings, use the % operator or the formatting method to format the strings. However, they cannot be generalized. You need to determine between + and %.

Avoid using the + and + = operators in the loop to accumulate strings. because the string is immutable, this will create unnecessary temporary objects and lead to quadratic instead of linear runtime. as an alternative, you can add each substring to the list and then use.joinConnection list. (You can also write each substring to onecStringIO.StringIOCache .)

11. Files and sockets
Close the file explicitly at the end of the file and sockets.
12. TODO comments
Use TODO annotations for temporary code. It is a short-term solution. It is not perfect, but it is good enough.
13. Import format
Each import must have an exclusive row.

The import should always be placed at the top of the file, after the module comments and document strings, before the global variables and constants of the module, the import should be grouped from the most general to the least General Order:

  1. Standard Library Import
  2. Third-party library Import
  3. Application Specified Import

In each group, the complete package path of each module should be sorted alphabetically, regardless of case.

Statement
Generally, each statement should have an exclusive row.

However, if the test results and test statements are placed in one row, you can also put them in the same row.

If it is an if statement, this can be done only when there is no else. In particular, nevertry/exceptThis is because try and try cannot be placed in the same row.

15. Access Control
In Python, you should replace trivial and unimportant access functions with public variables to avoid additional function call overhead. when adding more functions, you can use properties to maintain syntax consistency. (Translator's note: Object-oriented programmers who pay attention to encapsulation may be disgusted with this because they have been educated: All member variables must be private! Actually, it is a little troublesome. Try to accept the Pythonic philosophy)
16th. Name
module_name, package_name, ClassName, method_name, ExceptionName, function_name, GLOBAL_VAR_NAME, instance_var_name, function_parameter_name, local_var_name.

Names to be avoided

 
 
  1. Single character name, except counters and iterators.
  2. Hyphens (-) in the package/Module name (-)
  3. Name starting with and ending with a double underline (retained by Python, for example, _ init __)

Naming Conventions

 
 
  1. The so-called "Internal (Internal)" indicates that only the module is available, or, is protected or private within the class.
  2. Start with a single underscore (_) to indicate that the module variable or function is protected (not included when import * from is used ).
  3. Instance variables or methods starting with double underscores (_) indicate private in the class.
  4. Place related classes and top-level functions in the same module. Unlike Java, there is no need to restrict one class and one module.
  5. The class name should start with an upper-case letter (for example, CapWords, or Pascal style), but the module name should be underlined in lower case (for example, lower_with_under.py ). although many existing modules use CapWords. however, this is not encouraged because the module name happens to be the same as the class name.

Specifications recommended by Guido, father of Python

Type Public Internal
Modules Lower_with_under _ Lower_with_under
Packages Lower_with_under  
Classes CapWords _ CapWords
Exceptions CapWords  
Functions Lower_with_under () _ Lower_with_under ()
Global/Class Constants CAPS_WITH_UNDER _ CAPS_WITH_UNDER
Global/Class Variables Lower_with_under _ Lower_with_under
Instance Variables Lower_with_under _ Lower_with_under (protected) or _ lower_with_under (private)
Method Names Lower_with_under () _ Lower_with_under () (protected) or _ lower_with_under () (private)
Function/Method Parameters Lower_with_under  
Local Variables Lower_with_under  
17. Main
Even a file intended to be used as a script can be imported. in addition, a simple import should not cause the main function of the script to be executed, which is a side effect. the main function should be placed in a main () function.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.