Naming rules for Python coding specifications

Source: Internet
Author: User

1. Do not use lower-case letters 'l' (el), upper-case letters 'O' (oh), or lower-case 'I' as independent variable names. In some fonts, it is difficult to distinguish appeal letters from numbers (for example, O and 0, l and 1 ).
2. The Module name should be in lower case and should be named as short as possible. You can use underscores in the Module name to improve readability. The Package should be in lower case and should be named as short as possible, but underlines are not allowed. When an extension module written in C or C ++ has an accompanying Python module to provide a higher-level (for example, more object-oriented) interface, the C/C ++ Module name has a leading underline (for example, _ socket ).
3. The class name uses CapWords as the first letter. Internal classes use an additional leading underline.
4. Because the exception should be a class, the class naming conventions also apply to exceptions. However, you should add the suffix "Error" to the Exception name (if the exception is indeed an Error ).
5. For modules designed to be used through "from M import", the all mechanism should be used to prevent global variables from being exported; or the old convention should be used, add a leading underline to the global variables (You may want to indicate that these global variables are "module non-public ").
6. The function name should be in lower case. If necessary, words can be separated by underscores to improve readability. MixedCase is only allowed to be used in a context that is dominant in this style (for example, threading. py) to maintain backward compatibility.
7. The instance method always uses 'self 'as the first parameter. The class method always uses 'cls' as the first parameter. If the parameter name of a function conflicts with the reserved keyword, it is better to underline the parameter name than to use the abbreviation or wrong spelling. Therefore, "_ print" is better than "prnt. (It may be better to Use synonyms to avoid conflicts .)
8. Method Name and instance variable: Use the function naming rule: lowercase words. If necessary, use underscores to separate words to improve readability. Only use a leading underline for the non-public method and instance variables. To avoid conflicts with subclass naming, two leading underlines are used to trigger the naming Reorganization rules of Python. Python uses the class name to rename these names: if the class Foo has an attribute named _ a, it cannot be accessed with Foo. _. (Persistent users can still obtain access through Foo. _ Foo _ .) Generally, the double-leading underline is used only to avoid name conflicts with the attributes of the base class.
9. constants are defined in uppercase. If necessary, words can be separated by underscores to improve readability.

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.