Python Naming conventions

Source: Internet
Author: User
Tags access properties assert naming convention

Python identifiers

See also: Https://docs.python.org/3/reference/lexical_analysis.html?highlight=identifier#identifiers

In Python, identifiers consist of letters, numbers, underscores, and cannot use reserved words in Python.

In Python, all identifiers can include English, numeric, and underscore (_), but cannot begin with a number.

Identifiers in Python are case-sensitive.

Identifiers that begin with an underscore are of special significance. A class attribute that begins with a single underscore (_foo) cannot be accessed directly, and is accessed through the interface provided by the class and cannot be imported with "from XXX import *";

A double underscore (__foo) represents a private member of a class; a double underscore (__foo__) represents a special method-specific identifier for Python, such as __init__ (), which represents the constructor of a class.

Python reserved characters

The following list shows the reserved words in Python. These reserved words cannot be used as constants or variables, or as any other identifier name.

First look at the command line for Python's reserved words.

1 Import keyword 2 keyword.kwlist

Output: [' False ', ' None ', ' True ', ' and ', ' as ', ' assert ', ' Break ', ' class ', ' Continue ', ' Def ', ' del ', ' elif ', ' Else ', ' except ', ' Finally ', ' for ', ' from ', ' global ', ' if ', ' import ', ' in ', ' was ', ' lambda ', ' nonlocal ', ' not ', ' or ', ' Pass ', ' raise ', ' retur n ', ' Try ', ' while ', ' with ', ' yield ']

Reserved words Description Reserved words Description
False

The false value of the bool type. Built-in constant bool type false

Global

Defining Global Variables
None Built-in constants mean no If Conditional statements, used in conjunction with else, elif
True The true value of the bool type. Built-in constant bool type true Import Used to import modules, used in conjunction with from
and For expression arithmetic, logic and manipulation Inch Determine if the variable is in the sequence
As For type conversions Is Determine if a variable is an instance of a class
Assert Assertion, which is used to determine whether the value of a variable or conditional expression is true Lambda Define the number of anonymous rows
Break Execution of an interrupt loop statement Nonlocal Used to use an outer (non-global) variable in a function or other scope
Class Used to define classes Not Expression arithmetic, logical non-operation
Continue End this cycle to continue the next cycle Or Expression arithmetic, logic, or operation
Def Defining a function or method Pass Placeholder for an empty class, method, or function
Elif Conditional control statements, used in conjunction with IF, else Raise Exception throw operation
Esle Conditional control statements, used in conjunction with IF, elif Return Returning a calculated result from a function
Except Except contains the action code block after the exception capture with the try, finally combination Try Contains code blocks that may appear, used in conjunction with except, finally
Finally For exception statements, after exception capture, always execute the finally contained code block, used in conjunction with try, except While While loop
For For Loop statement With Simplify Python's statements
From Used to import modules, used in conjunction with import Yield Return a value from a function sequentially

naming

See also: https://www.python.org/dev/peps/pep-0008/

Try to avoid the use of hard-to-distinguish letters "L", "O" number "1", "0".

1. Variable name, package name, module name

variable names are usually composed of letters, underscores, package names, module names are usually short lowercase letters, improve the readability of the module name can also contain underscores, package hits are not recommended to use underscores.

# Filename:rulemodle.py  "rule information"

The first line is the program Comment line, which declares the module name, the module name is in lowercase letters, or the module name is not specified, and the file with the. py suffix is a module, and the filename is the module name.

2. Class name, object name

The first letter of the class name is capitalized, and the other letters are lowercase. The object name is in lowercase letters. Classes used internally are underlined before the class name.

The properties and method names of the class are prefixed with an object, and the object passes the operator "." Access properties and methods. The private variables and private methods of the class are prefixed with two underscores.

 classStudent:#class name, first letter capitalization     __name="'   #private variables, starting with __     def __init__(self,name):#Self is equivalent to this in JavaSelf.__name=namedefGetName (self):returnSelf.__name if __name__=="__main__": Student= Student ("Zhaowei")#object name, Small letter     Print(Student.getname ())

3. Name of function

Function names are usually lowercase and, if necessary, use underscores to differentiate words to improve readability.

Mixedcase (the first letter lowercase, followed by the first letter of words to split the word) is only suitable for maintaining style and backwards compatibility.

The imported function is prefixed with the module name.

ImportRandom#Import ModuledefComparenum (NUM1,NUM2):#Defining Functions    if(Num1 >num2):Print("num1:%d > num2:%d"%(num1,num2))elif(NUM1 = =num2):Print("Num1:%d = = num2:%d"%(num1,num2))Else:        Print("Num1:%d < num2:%d"%(num1,num2)) Num1= Random.randrange (1,9)#function that invokes the modulenum2 = Random.randrange (1,9) Comparenum (num1,num2)

4. Exception name

Because exceptions are also classes, the naming conventions of classes apply here. The difference is that if the exception is actually an error, you need to use the error suffix after the exception name

5. Global variable Name

Global variable Name
We assume that these variables are used internally within the module. Naming follows a rule that is basically the same as a function's naming convention.

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.