Python naming rules

Source: Internet
Author: User

Python naming rules

 

Python identifier

See: https://docs.python.org/3/reference/lexical_analysis.html? Highlight = identifier # identifiers

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

In python, all identifiers can contain English letters, numbers, and underscores (_), but cannot start with a number.

The identifiers in python are case sensitive.

The identifier starting with the following line is of special significance. A class attribute that starts with a single underscore (_ foo) represents a class attribute that cannot be accessed directly. The class must be accessed through the interface provided by the class, and cannot be imported using "from xxx import;

(_ Foo) starting with a double underline represents a private member of the class; (_ foo _) Starting with and ending with a double underline represents a special identifier for special methods in python, for example, _ init _ () indicates the constructor of the class.

Python reserved characters

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

First, let's take a look at the Reserved Words in Python on the command line.

 

1 import keyword2 keyword.kwlist

 

Output: ['false', 'none', 'true', 'and', 'as', 'assert ', 'Break', 'class', 'contine ', 'def', 'del ', 'elif', 'else', 'partition t', 'Finally', 'for ', 'from', 'global', 'if ', 'import', 'in', 'is ', 'lambda', 'nonlocal', 'not ', 'or', 'pass', 'raise', 'Return ', 'try', 'wait', 'with', 'yield ']

Reserved Words Description Reserved Words Description
False

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

Global

Define global variables
None Built-in Constant indicates no If Conditional statement, used in combination with else and elif
True The true value of the bool type. Built-in constant bool type true Import Used in the import module and in combination with from
And Used for expression operations, logic, and operations In Judge whether the variable is in the sequence
As Used for type conversion Is Determines whether the variable is an instance of a class.
Assert Assertion, used to determine whether the value of a variable or conditional expression is true Lambda Defines the number of anonymous rows
Break Interrupt loop statement execution Nonlocal Used to use outer (non-Global) variables in functions or other scopes
Class Used to define a class Not Expression operation, logical non-operation
Continue End this cycle and continue the next cycle Or Expression operation, logic or operation
Def Define functions or methods Pass A placeholder for an empty class, method, or function.
Elif Conditional Control statement, used in combination with if and else Raise Exception throwing
Esle Conditional Control statement, used in combination with if and elif Return Returns the calculation result from the function.
Except When T contains the operation code block after exception capture, it can be used in combination with try and finally. Try Contains code blocks that may appear. It can be used with blocks T and finally.
Finally Used for exception statements. After exception capture, you must always execute the finally contained code block, which is used in combination with try and try. While While Loop
For For Loop statement With Simplify Python statements
From Used in the import module. used in combination with import Yield Return values from functions in sequence

 

 

Name

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

Avoid using hard-to-distinguish letters "l", "o" Numbers "1", and "0 ".

1. variable name, package name, Module name

Variable names are generally composed of letters and underscores. The package name and module name generally use short lowercase letters. To improve readability, the module name can also contain underscores. We recommend that you do not use underscores when you hit a package.

#Filename:rulemodle.py_rule = "rule information"

The first line is the program comment line, which is used to declare the module name. The module name can be in lowercase letters, or the module name can be left unspecified. the file with the py suffix is a module, and the file name is the module name.

2. Class Name and Object Name

The first letter of the class name is in upper case, and other letters are in lower case. The object name must contain lowercase letters. The internal class is underlined before the class name.

Class attributes and method names are prefixed with objects. Objects access attributes and methods through the operator. The private variables and private methods of the class are prefixed with two underscores.

Class Student: # class name, capital letter _ name = ''# private variable, start with _ def _ init _ (self, name ): # self is equivalent to this self in java. _ name = name def getName (self): return self. _ name if _ name _ = "_ main _": student = Student ("zhaowei") # Object name, lowercase letter print (student. getName ())

3. Function Name

Function names are usually in lower case. If necessary, use underscores to distinguish words to improve readability.

MixedCase (lowercase letters, followed by uppercase words) is only applicable to style and downward compatibility.

The imported function uses the module name as the prefix.

Import random # import module def compareNum (num1, num2): # define the function 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 () # Call the module's function num2 = random. randrange (1, 9) compareNum (num1, num2)

4. Exception name

Because exceptions are also classes, the naming conventions of classes also apply here. The difference is that if an 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 all these variables are used inside the module. The naming rules are basically the same as those of the function.

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.