Python naming rules

Source: Internet
Author: User

I,Package name, Module name, local variable name, function name

All lower case + underlined hump

Example: this_is_var

2. Global Variables

Full upper case + underlined hump

Example: GLOBAL_VAR

Iii. Class Name

Upper-case hump

Example: ClassName ()

4. Underline

Example for 1:

The variables var_1, _ var_2, _ var_3 are defined in the module_1 module.

 

The code in the module_2 module is as follows:

        module_1  *       _var_2       

An error will be reported when you execute lines 6 and 7, because they will not be imported.

Since it is a weak internal identifier, there is still a way to use it, just import it separately:

    module_1  *     module_1  _var_2,      _var_2     

Example for 2:

                      self.var_1 = 1         self._var_2 = 2         self. = 3       ==     obj =                obj.  
 = 1= 2 = 3             self.             === obj.get_var_3()  

4. Other notes

  • Do not use start letters to identify the variable type (such as iValue) Like in c, because python determines the type only when interpreting it.
  • Because exceptions are also a class, they follow the class naming rules. In addition, if an exception actually refers to an Error, Use "Error" as the suffix.
  • Try to use full spelling words for naming. There are two abbreviations: Common abbreviations, such as XML and ID. Only the first letter, such as XmlParser, should be capitalized. The name contains long words, which are abbreviated to a word. In this case, we should use the abbreviated form of conventions. For example, function stands for fn, text stands for txt, object stands for obj, count stands for cnt, and number stands for num.
  • The first parameter of the class instance method uses self, and the first parameter of the class method uses cls

5. Finally, some English instructions are used as remarks.

Since module names are mapped to file names, and some file systems are

 

 

 

 

 

 

 

 

 

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.