Analysis of Python programming specifications

Source: Internet
Author: User

Before learning the Python programming language, we must first understand some major coding specifications of this programming language. Today, we will give you a detailed introduction to the Python programming specifications. I hope you can get some help to facilitate future learning and application.

  • Analysis of the Creation method and particularity of Python tuples
  • Parse the correct operation method when Python calls the zip command
  • Explanation of common examples of Python ure Module
  • Explanation of Python plug-in PyDev's correct configuration method
  • Sample Code for calling Python functions in C

Indentation of Python programming specifications

Use the default value of Python-mode in Emacs: four spaces and one indent level. For really old code, you do not want to create confusion, you can continue to use the 8-space tab (8-space tabs ). Emacs Python-mode automatically discovers the main indentation level in the file, and sets the indent parameter accordingly.

Python programming specifications tab or space

Do not mix tabs and spaces. The most popular Python indent method is to use only spaces, followed by only tabs. Code mixed with tabs and space indentation will be converted to use only spaces. (In Emacs, select the entire buffer and Press ESC-x to remove tabs .) When the Python command line interpreter is called, The-t option can be used to warn against the combination of tabs and spaces in the Code. When the-tt option is used, the warning will become an error. These options are highly recommended.

For new projects, we strongly recommend that you only use spaces instead of tabs. Many editors have features that make it easy to implement (in Emacs, check that indent-tabs-mode is nil ).

The maximum length of the line of the Python programming Specification

There are still many devices around it that are limited to 80 characters per line: And the window is limited to 80 characters. Make it possible to place multiple windows side by side. Using the default folding method on these devices looks ugly. Therefore, limit all rows to a maximum of 79 characters (Emacs must accurately limit the row length to 80 characters), and discharge large pieces of text (document strings or comments) to the order ), we recommend that you set the length to 72 characters.

The preferred way to fold long rows is to use parentheses, square brackets, and row continuation in curly brackets supported by Pyhon. If needed, you can add an extra pair of parentheses around the expression, but sometimes it looks better to use a backslash to confirm that the continuation row is properly indented.

The Python-mode of Emacs must be correct. Some examples:

 
 
  1. #!Python  
  2. class Rectangle(Blob):  
  3. def __init__(self,width,height,color='black',
    emphasis=None,highlight=0):  
  4. if width == 0 and height == 0 and \  
  5. color == 'red' and emphasis == 'strong' or \  
  6. highlight > 100:  
  7. raise ValueError, "sorry, you lose"  
  8. if width == 0 and height == 0 and (color == 'red' or  
  9. emphasis is None):  
  10. raise ValueError,"I don't think so"  
  11. Blob.__init__(self,width,height,color,emphasis,highlight) 

Empty line of Python programming specifications

Use two blank lines to separate top-level functions and class definitions. Use a single blank line to separate methods in the class. Additional blank lines can be used to (conservatively) separate groups composed of related functions, empty rows can be omitted in the middle of a group of related single sentences. (For example, a set of dummy elements ).

When empty rows are used to define the segmentation method, there must be an empty row between the 'class' row and the first method definition. Exercise caution when using empty rows in a function to represent a logical section. Python accepts the contol-L (I .e. ^ L) Tab character as a space: Emacs (and some printing tools) and regards this character as a page delimiter. Therefore, in your file, you can use them to pagination related fragments.

Encoding of Python programming specifications

The code in the Python Core Release must always use ASCII or Latin-1 encoding (also known as ISO-8859-1), and files that use ASCII do not have to have encoded cookies, latin-1 is used only when the comment or document string involves the author's name Latin-1:

In addition, using the \ x escape character is the preferred method to include non-ASCII (non-ASCII) data in a string.

Some files in the test suite used as the pep 263 implementation code are exceptions.

Import of Python programming specifications

Import (Imports) in a separate row, for example:

No: import sys, OS

Yes: import sys

Import OS

But this is also possible:

From types import StringType, ListType

Imports is usually placed at the top of the file, only after the module comments and document strings, before the global variables and constants of the module. Imports should be sequentially grouped:

1. Import the standard library (Imports)

2. Import the relevant main package (major package) (that is, all email packages will be imported later)

3. Import specific applications (imports)

You should place an empty line between each group of imports. Relative imports are not recommended for internal package imports, and absolute paths of all imports should be used.

When importing a class from a module that contains the class, you can generally write it as follows:

From MyClass import MyClass

From foo. bar. YourClass import YourClass

If this write causes a local Name Conflict, write

Import MyClass

Import foo. bar. YourClass

Use "MyClass. MyClass" and "foo. bar. YourClass. YourClass"

Spaces in expressions and statements

Guido does not like spaces in the following places:

Placed next to parentheses, square brackets, and curly brackets, for example: "spam (ham [1], {eggs: 2 })". Always write it as "spam (ham [1], {eggs: 2 })".

Followed by commas, semicolons, or colons, such:

"If x = 4: print x, y: x, y = y, x ". Always write it

"If x = 4: print x, y: x, y = y, x ".

Close to the open parenthesis (open parenthesis) in the parameter list of function calls, for example, "spam (1 )". Always write it as "spam (1 )".

Prefixed to the index or slice before the opening brackets, for example:

"Dict ['key'] = list [index]". Always write it as "dict ['key'] = list [index]".

More than one space is used to side the value assignment (or other) operator with other operators, such:

 
 
  1. #!Python  
  2. x= 1 
  3. y= 2 
  4. long_variable = 3 

Always write it

 
 
  1. #!Python  
  2. x = 1 
  3. y = 2 
  4. long_variable = 3 

The above is an introduction to the Python programming specifications.

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.