Python coding style

Source: Internet
Author: User

Now that you can write a longer, more complex Python program, it's time to discuss the coding style . Most languages can be written (or, more clearly, formatted ) in several different styles. Some are better read than others. It's a good idea to make your code easier for others to read, and it's helpful to develop a good coding style.

For Python, PEP 8 introduces style guidance that most projects follow. It gives a highly readable, visually-friendly coding style. Every Python developer should read and most of the points will help you:

  • Use 4 spaces to indent rather than TAB.

    Between small indents (which can be nested deeper) and large indents (easier to read), 4 spaces are a good compromise. TAB has caused some confusion, preferably deprecated.

  • Wrap the line to make sure it doesn't exceed 79 characters.

    This helps small-screen users to read, or allows large displays to display several code files side-by-side.

  • Use empty lines to separate functions and classes, and large chunks of code in functions.

  • If possible, comment exclusive line

  • Working with document Strings

  • Place the space on either side of the operator and behind the comma, but no space is added to the brackets: a = f (1, 2) + g (3, 4) .

  • Uniform functions and class naming.

    The recommended class names are named with the hump , and the function and method names are underlined in lowercase _ and _ . Always use self as the first parameter of the method (for knowledge of classes and methods see First Class ).

  • Do not use fancy coding if the purpose of your code is to be in an internationalized environment. Python by default, UTF-8, even ordinary ASCII always works best.

  • Also, do not use identifiers that are non-ASCII characters unless the code is read or maintained in a different language.

Python coding style

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.