Home > Backend Development > Python Tutorial > Summary of python writing specifications and naming conventions

Summary of python writing specifications and naming conventions

不言
Release: 2019-01-25 09:55:52
forward
2060 people have browsed it

What this article brings to you is a summary of python writing specifications and naming conventions. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Writing specifications

  • Each import statement only imports one module, try to avoid importing multiple modules at once module.

  • Do not add a semicolon ";" at the end of the line, and do not use a semicolon to put two commands on the same line.

  • It is recommended that each line should not exceed 80 characters. If it exceeds, it is recommended to use parentheses to implicitly connect multiple lines of content, and it is not recommended to use backslashes to connect

  • Using necessary blank lines can increase the readability of the code. Generally, there are two blank lines between top-level definitions (such as function or class definitions), and one blank line between method definitions. Alternatively, a blank line can be used to separate certain functions.

  • Normally, it is recommended to use empty shells to separate both sides of operators, function parameters, and commas.

  • You should avoid using the and = operators in a loop to accumulate strings. This is because strings are immutable and doing so creates unnecessary temporary objects. The recommended approach is to add each substring to the list and then use the join() method to join the list after the loop ends.

  • Appropriate use of exception handling structures improves program fault tolerance, but you cannot rely too much on exception handling structures. Appropriate explicit judgment is still necessary.

Naming convention

  • #Module names should be as short as possible and use all lowercase letters. You can use underscores to separate multiple names. letter.

  • Keep the package name as short as possible and use all lowercase letters. Underscores are not recommended.

  • Class names are capitalized with the first letter of the word.

  • The classes inside the module are composed of Pascal style class names with underscores.

  • The naming rules for functions, class attributes and methods are similar to those of modules, all using lowercase letters, and multiple letters separated by underscores.

  • Use all uppercase letters when naming constants, and underscores can be used.

  • Module variables or functions starting with an underscore are protected and cannot be imported when importing from the module using the import *from statement.

  • Instance variables or methods starting with an underscore are private to the class.

The above is the detailed content of Summary of python writing specifications and naming conventions. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template