Python-Naming conventions

Source: Internet
Author: User
One, package name, module name, local variable name, function name

All-lowercase + underline hump

Example:this_is_var

Second, global variables

All caps + Underline hump

Example:global_var

Third, class name

First-Letter Capital Hump

Example:classname ()

Four, about the underline

Starting with a single underscore, which is a weak internal use identifier, from M import *, the object will not be imported (Python is all objects).

A variable name that begins with a double underscore, and is used primarily for class-internal identity classes that are private and not directly accessible. Used in the module, see the previous bar.

A naming method that starts with a double underscore and a double-underlined truncation try not to use it.

Example for 1:

Variables defined in the Module_1 module var_1, _var_2, __var_3

#module_1

Var_1

_var_2

__var_3

The code in the Module_2 module is as follows:

#module_2_error "' from  module_1 import *      print var_1  print _var_2  #将报错  Print _ '. _var_3  #将报错


Execution to 6th, 7 will result in an error, because objects that begin with the underscore will not be imported.

Since the weak internal use of the logo, there is still a way to use, just import it alone:

#module_2_solution from    module_1 import *  # Imports all objects that are not preceded by an underscore from    module_1 import _var_2, __var_3 # Explicitly import an underscore beginning with an object    print var_1 print _var_2 # no error print __var_3  # no error


Example for 2:

Variables that start with the #module_error ' double underscore cannot be directly accessed '    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  # here will be an error


You need to define a function to get a variable that starts with a double underscore

#module_solution  ' need to define a function to get the variable beginning with a double underscore ' '  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 more errors


Four, the other should pay attention to

Do not use the first letter to identify the variable type (such as Ivalue) in a language like C, because Python determines the type when it is interpreted.

Because exceptions are also a class, follow the naming conventions for classes. In addition, if the exception actually refers to an error, you should use "error" as the suffix.

The name should try to use the full spelling of the word, the abbreviation for the following two kinds of cases: commonly used abbreviations, such as XML, ID, etc., should be named only the first letter, such as Xmlparser. The name contains long words, and a word is abbreviated. You should use the abbreviated method of the contract idiomatic. For example: function abbreviation is FN, text abbreviation is TXT, object abbreviation is obj, count abbreviation is CNT, number abbreviation is num, etc.

Class instance method The first argument uses self, and the first parameter of the class method uses the CLS

  • Related Article

    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.