Home  >  Article  >  Backend Development  >  Python - naming conventions

Python - naming conventions

高洛峰
高洛峰Original
2016-10-19 17:08:271545browse

One, package name, module name, local variable name, function name

All lowercase + underscore camel case

example: this_is_var

Second, global variable

All uppercase + underscore camel case

example: GLOBAL_VAR

, the class name

capitalized camel case

example: ClassName()

Fourth, about the underscore

starting with a single underscore is a weak internal use identifier, when from M import *, the object will not be imported (python Everything is an object).

Variable names starting with double underscores are mainly used to identify the class internally as private and cannot be accessed directly. See the previous item for use in modules.

Try not to use the naming method that starts with a double underscore and ends with a double underscore. This is the identifier.

example for 1:

module_1 defines variables var_1, _var_2, __var_3 in the module

#module_1

var_1

_var_2

The code in the __var_3

module_2 module is as follows:

#module_2_error
 '''
 以下划线开头的变量不会别导入
 '''
  from module_1 import *
    
  print var_1
  print _var_2  #将报错
  print __var_3  #将报错


When executed to lines 6 and 7, an error will be reported, because any object starting with an underscore will not be imported.

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

#module_2_solution
   
 from module_1 import *  # 导入所有的不是下划线开头的对象
   
 from module_1 import _var_2, __var_3 # 显式导入下划线开头的对象
   
 print var_1
 print _var_2 # 不会报错
 print __var_3  # 不会报错


example for 2:

#module_error
 '''
 双下划线开头的变量不能被直接访问
 '''
   
 class MyClass():
     def __init__(self):
         self.var_1 = 1
         self._var_2 = 2
         self.__var_3 = 3
       
 if __name__=="__main__":
     obj = MyClass()
     print obj.var_1
     print obj._var_2
     print obj.__var_3  # 这里将会出错


Need to define a function To get the variables starting with a double underscore

#module_solution
  
'''
需要定义函数来获取双下划线开头的变量
'''
  
class MyClass():
    def __init__(self):
        self.var_1 = 1
        self._var_2 = 2
        self.__var_3 = 3
          
    def get_var_3(self):
        return self.__var_3
          
      
  
if __name__=="__main__":
    obj = MyClass()
    print obj.var_1
    print obj._var_2
    print obj.get_var_3()  # 不会再报错


Four, other things to note

Don’t use the first letter to identify the variable type (such as iValue) like in languages ​​such as C, because python interprets it Only then determine the type.

Because exception is also a class, it follows the naming rules of the class. Additionally, if the exception actually refers to an error, it should be suffixed with "Error".

You should try to use fully spelled words when naming. There are two types of abbreviations: commonly used abbreviations, such as XML, ID, etc., only the first letter should be capitalized when naming, such as XmlParser. The name contains long words, and a certain word is abbreviated. In this case, the conventional abbreviation should be used. For example: function is abbreviated as fn, text is abbreviated as txt, object is abbreviated as obj, count is abbreviated as cnt, number is abbreviated as num, etc.

The first parameter of class instance method uses self, and the first parameter of class method uses cls


Statement:
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