"Go" python variable naming specification

Source: Internet
Author: User
Tags instance method

Python source code and some other books, naming a variety of personalities, not a more uniform naming conventions. Then summed up some, for reference.

Module Name:

Modules should be named as short as possible, all lowercase, and can be underlined to enhance readability when the module is named. The same package name should also be the case, although it does not encourage underlining.

The main consideration is that the module name is corresponding to the folder, so it is necessary to consider some naming rules of the file system, such as UNIX system is sensitive to case, and the long file name will affect its normal use in Windows\mac\dos system.

Usually lowercase letters, the words are separated by _

ad_stats.py

Package Name:

Same as the module name

Class Name:

Almost invariably, class names are preceded by a specification of the initials (Pascal naming style). The class name that begins with _ single underscore is used internally, and the from M import * is not imported by default.

Capitalize the first letter of the word

Adstats

Configutil

global variable names (class variables, equivalent to static variables in Java):

Uppercase letters, dividing between words with _

Number

Color_write

For from M Import * Import statements, if you want to prevent global variables within the import module from using the old specification, add a leading underscore to the global variable.

* NOTE *: You should avoid using global variables

Common variables:

Lowercase letters, dividing between words with _

This_is_a_var

* Note *:

1. Neither the class member variable nor the global variable uses the M or G prefixes.

2. Private class members are identified with a single underscore prefix, multiple definitions expose members, and less private members are defined.

3. Variable names should not have type information, because Python is a dynamic type language. such as Ivalue, Names_list, dict_obj, etc. are not a good name.

Instance variable:

Start with _, the rest is the same as the normal variable

_price

_instance_var

Private instance variable (external access will error):

Starts with __ (2 underscores), and the other is the same as the normal variable

__private_var

Proprietary variables:

__ Begins, __ ends, is usually python's own variable, do not name it in this way

__doc__

__class__

Common functions:

As with normal variables:

Get_name ()

Count_number ()

Ad_stat ()

Private function (external access will error):

Starts with __ (2 underscores), and the rest is the same as the normal function

__get_name ()

Filename:

Full lowercase, underline available

* Note *: Mixed case is only allowed to be used when this style has taken advantage, in order to remain backwards compatible.

Parameters for functions and methods:

Always use "self" as the first parameter of an instance method. Always use "CLS" as the first parameter of a class method.

If a function's parameter name conflicts with reserved keywords, it is usually better to use a suffix underline than to use abbreviations or strange spellings.

Constant:

The constant name is all letters capitalized, and each word is connected by an underscore, such as max_overflow,total.

Abnormal

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.

Abbreviation

The name should use all spelling words as far as possible, the abbreviation is as follows two kinds of things:

1. Commonly used abbreviations, such as XML, ID, and so on, should be named with only the first letter, such as Xmlparser.

2. 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 for TXT

The object abbreviation is obj

Count abbreviation for CNT

Number is abbreviated to NUM, and so on.

Leading suffix underline summary

A leading underscore: represents a non-public.

One suffix underscore: avoid keyword collisions.

Two leading underscores: used when naming a class property that causes a name conflict.

Two leading and suffix underscores: "Magic" (with special purpose) objects or attributes, such as __init__ or __file__. Never create names like this, just use them.

* NOTE *: There is some controversy about the use of underscores.

PYTHON specifies special variables with underscores as variable prefixes and suffixes.

_xxx cannot be imported with ' from module import * ' (equivalent to private)

__xxx__ System Definition Name

Private variable name in __xxx class (equivalent to protect)

Core style: Avoid using underscores as the start of variable names.

Because underscores have special meanings for the interpreter and are symbols used by built-in identifiers, we recommend that programmers avoid using underscores as the starting point for variable names. In general, variable name _xxx is considered "private" and cannot be used outside of a module or class. When a variable is private, it is good practice to use _xxx to represent variables. A member variable that starts with a "single underline" is called a protection variable, meaning that only class objects and subclass objects can access these variables themselves;

A "double underline" begins with a private member, meaning that only the class object can access it, and the child object cannot access the data.

A class attribute that begins with a single underscore (_foo) cannot be accessed directly, and is accessed through the interface provided by the class, cannot be imported with a "from XXX import *", and a double underscore (__foo) represents a private member of the class; A double underscore that starts and ends (__foo__ Represents a special method-specific identifier for Python, such as __init__ (), which represents the constructor of a class.

Specific naming methods

Mainly refers to the __xxx__ form of the system reserved word naming method. This type of naming can also be used in projects, meaning that variables of this form are read-only and this form of class member functions is not overloaded as much as possible. Such as

Class Base (object):

def __init__ (self, id, parent = None):

self.__id__ = ID

self.__parent__ = Parent

def __message__ (self, MsgId):

# ... Slightly
Among them, __id__, __parent__ and __message__ all adopt the system reserved word naming method.

__ Double underline ends with double underscore: refers to "magic" objects or attributes that are contained in namespaces that the user cannot control, such as __name__, __doc__, __init__, __import__, __file__, and so on for class members. It is recommended that you never apply such a naming method to your own variables or functions.

Single Underline End _: Just to avoid naming conflicts with Python keywords

_ Single underline: weak "Internal use" identification, such as: "From M import *", will not import all objects beginning with the underscore, including packages, modules, members

"Go" python variable naming specification

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.