Python identifier naming convention

Simple to understand, the identifier is a name, as if each of us has his own name, its main role is as a variable, the function names, classes, modules, and other objects.

Python identifier name is not random, but to comply with certain rules of command, such as: marble platform production plant

  1. Identifier is a character (A ~ Z and a ~ z), underscore and numbers, but not the first character is a number.
  2. Python identifier and not the same as a reserved word. Reserved words, subsequent chapters will be described in detail.
  3. In Python identifiers, can not contain spaces, @,% and $ other special characters.
    For example, listed below are valid identifier:

    UserID
    name
    mode12
    user_age

    The following names are not valid identifiers:

    4word # can not start with a number
    try #try is a reserved word and should not be used as identifiers
    $ money # can not contain special characters

  4. In Python, the identifiers of the letter is strictly case-sensitive, that is, the same two words, if the format is not the same size, meaning multi representatives are also completely different. For example, between the following three variables, that is completely independent, unrelated, are independent individuals among each other.

    number = 0
    Number = 0
    NUMBER = 0

  5. Python language to underscore identifiers have special meaning, such as:
    • A single identifier begin with an underscore (e.g., the _width), represents a class attributes are not directly accessible, it can not be introduced by way from ... import *;
    • An identifier (e.g. __add) beginning with the double underline indicates the private members of the class;
    • Double-underlined as the beginning and end identifiers (e.g. __init__), is a specific identifier.
    Therefore, unless a particular scene needs, you should avoid using identifiers that begin with an underscore.


Also note that, Python as an identifier allows the use of Chinese characters, for example:

C language Chinese network = "http://c.biancheng.net"

But we should try to avoid using Chinese characters as an identifier, which will meet a lot of wonderful work to avoid mistakes.

Identifier names, in addition to compliance with the above several of these rules, different scenarios identifier, its name also has certain norms to follow, such as:

  • When the identifier is used as the module name, it should be short, and all lowercase letters, can use underscores division multiple letters, e.g. game_mian, game_register like.
  • When the name is used as an identifier of the package, it should be short, but also all lowercase letters, underline not recommended, e.g. com.mr, com.mr.book like.
  • When used as an identifier for the class name should be uppercase first letter of the word. For example, the definition of a class library, can be named Book.
  • Inside the module class name, it can be used "Underline capitalized +" form, such as _Book;
  • Function name, class property and method names, all lowercase letters shall be divided among a plurality of words underlined;
  • Constant name should use all capital letters, you can use an underscore split between words;


Some readers may ask, if you do not comply with these norms, what will happen? The answer is that the program can still run, but the benefits of following the above specification that can be more intuitive understanding of the meaning of the code represent, to the Book class as an example, we can easily guess this type of book about, although the class name change It is a (or another) will not affect the program running, but usually do not do. 

Guess you like

Origin www.cnblogs.com/furuihua/p/12539327.html