Python Naming conventions

Source: Internet
Author: User

Python Naming conventions

Google Python Naming Conventions

Module_name, module

Package_name, Bag

ClassName, class

Method_name, method

Exceptionname, abnormal

Function_name, function

Global_var_name, global variables

Instance_var_name, example

Function_parameter_name, Parameters

Local_var_name. This variable

Class

Always capitalize the word string with the first letter. such as MyClass. The inner class can use additional leading underscores.

function & Method

lowercase + underline

* 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
If the parameter name of a function conflicts with a reserved keyword, a suffix is usually used to underline

Global variables
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

Variable

lowercase, connecting each word by an underscore. such as color = White,this_is_a_variable = 1
* 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.
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.

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

Abnormal
Use "Error" as the suffix.

Filename
Full lowercase, underline available

Package
It should be a short, lowercase name. If the underline can improve readability can be added. such as MyPackage.

Module
Same as the specification of the package. such as MyModule.

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
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 diagrams) 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.

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):
Among them, __id__, __parent__ and __message__ all adopt the system reserved word naming method.

Transferred from: https://www.cnblogs.com/EmptyRabbit/p/7679093.html

Python Naming conventions

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.