Package name, module name, local variable name, function name, all lowercase + underlined camper, 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
It must start with a single underline and is a weak internal identifier. when from M import * is used, this object is not imported (everything in python is an object ).
Variable names starting with double underscores (_) are mainly used for private class identifiers inside the class and cannot be accessed directly. For details about how to use this module, see the previous one.
This is an identifier.
Example for 1:
The variables var_1, _ var_2, _ var_3 are defined in the module_1 module.
# Module_1
Var_1
_ Var_2
_ Var_3
The code in the module_2 module is as follows:
# Variables starting with the le_2_error ''' are not imported into ''' from module_1 import * print var_1 print _ var_2 # print _ var_3 # An error is reported.
An error will be reported when you execute row 6 and 7, because all objects starting with the following line will not be imported.
Since it is a weak internal identifier, there is still a way to use it, just import it separately:
# Module_2_solution from module_1 import * # import all objects not starting with an underscore from module_1 import _ var_2, _ var_3 # explicitly import the object starting with an underscore print var_1 print _ var_2 # No error print _ var_3 # No error
Example for 2:
# Variables starting with module_error ''' cannot be directly accessed by ''' 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 # errors will occur here
You need to define a function to obtain variables starting with double underscores.
# Module_solution ''' you need to define a function to obtain the variable '''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 () # No error will be reported
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