Python
Python是一種面向對象、解釋型的程序設計語言。
這是一個識記性的頁面,詳細內容請參考此頁面的子頁面
目录 |
1 Lexical analysis
files must be analysed into tokens.
1.1 Line Structure
files are divided into logical lines.
1.2 Logical Lines
represented by the token NEWLINE.
1.3 Physical Lines
換行符
1.4 Comments
1.5 Encoding declarations
文件開頭的coding標識
1.6 Explicit line joining
\
1.7 Implicit line joining
(),{},[]
1.8 Indentation
is used to determine the grouping of statements.
2 Identifiers and keywords
2.1 Reserved classes of identifiers
__*__, __*
3 Literals
constant values of some built-in types.
3.1 String and Bytes literals
stringliteral ::= [stringprefix](shortstring | longstring) stringprefix ::= "r" | "R" shortstring ::= "'" shortstringitem* "'" | '"' shortstringitem* '"' longstring ::= "'''" longstringitem* "'''" | '"""' longstringitem* '"""' shortstringitem ::= shortstringchar | stringescapeseq longstringitem ::= longstringchar | stringescapeseq shortstringchar ::= <any source character except "\" or newline or the quote> longstringchar ::= <any source character except "\"> stringescapeseq ::= "\" <any source character> bytesliteral ::= bytesprefix(shortbytes | longbytes) bytesprefix ::= "b" | "B" | "br" | "Br" | "bR" | "BR" shortbytes ::= "'" shortbytesitem* "'" | '"' shortbytesitem* '"' longbytes ::= "'''" longbytesitem* "'''" | '"""' longbytesitem* '"""' shortbytesitem ::= shortbyteschar | bytesescapeseq longbytesitem ::= longbyteschar | bytesescapeseq shortbyteschar ::= <any ASCII character except "\" or newline or the quote> longbyteschar ::= <any ASCII character except "\"> bytesescapeseq ::= "\" <any ASCII character>
3.2 Integer literals
integer ::= decimalinteger | octinteger | hexinteger | bininteger
decimalinteger ::= nonzerodigit digit* | "0"+
nonzerodigit ::= "1"..."9"
digit ::= "0"..."9"
octinteger ::= "0" ("o" | "O") octdigit+
hexinteger ::= "0" ("x" | "X") hexdigit+
bininteger ::= "0" ("b" | "B") bindigit+
octdigit ::= "0"..."7"
hexdigit ::= digit | "a"..."f" | "A"..."F"
bindigit ::= "0" | "1"
3.3 Floating point literals
floatnumber ::= pointfloat | exponentfloat
pointfloat ::= [intpart] fraction | intpart "."
exponentfloat ::= (intpart | pointfloat) exponent
intpart ::= digit+
fraction ::= "." digit+
exponent ::= ("e" | "E") ["+" | "-"] digit+
3.4 Imaginary literals
imagnumber ::= (floatnumber | intpart) ("j" | "J")
4 Data model
4.1 Objects, values and types
objects一旦出生不能再改變,value是它的值,types決定了它支持的操作,types也是不能改變的。value可以改變的objects是mutable, 否則immutable。
reference是objects的名字,reference也可以指向其他objects.
containers是一種包含其他對象引用的對象,containers是否mutable與其內對象是否mutable是不同的,即使containers immutable, 但其內對象mutable,當期內對象改變了,containers也改變了。
4.2 The standard type hierarchy
4.2.1 None
4.2.2 NotImplemented
4.2.3 numbers.Number
immutable, int, real, complex
4.2.4 Sequences
[],slice, len()
immutable: Strings, Tuples, Bytes
mutable: Lists, Bytes array.
4.2.5 Set types
immutable, no index, len
Set, Frozen Set
4.2.6 Mappings
mutable, dict[key] = value
4.2.7 Callable types
4.2.7.1 User-defined functions
__doc__, __name__, __default__, __code__, __global__,__dict__,__module__
4.2.7.2 Instance methods
__func__, __self__, __module__
4.2.7.3 Generator functions
__next__()
4.2.7.4 Built-in functions
__name__, __doc__, __module__
4.2.8 Modules
__global__, __dict__
4.3 Special method names
4.3.1 Basic customization
__new__, __init__, __del__, __repr__, __str__, __bytes__, __format__
__lt__, __le__, __eq__,__ne__,__gt__,__ge__
__hash__, __bool__
4.3.2 Customizing attribute access
__getattribute__, __getatt__, __setattr__, __delattr__, __dir__
4.3.3 Emulating callable objects
__len__, __getitem__, __setitem__, __delitem__, __iter__, __reversed__, __contains__,
5 Execution model
5.1 Naming and binding
name, binding, block, scope
5.2 Exceptions
6 Expressions
7 Compound statements
compound_stmt ::= if_stmt
| while_stmt
| for_stmt
| try_stmt
| with_stmt
| funcdef
| classdef
suite ::= stmt_list NEWLINE | NEWLINE INDENT statement+ DEDENT
statement ::= stmt_list NEWLINE | compound_stmt
stmt_list ::= simple_stmt (";" simple_stmt)* [";"]
7.1 The if statement
if_stmt ::= "if" expression ":" suite
( "elif" expression ":" suite )*
["else" ":" suite]
7.2 The while statement
while_stmt ::= "while" expression ":" suite
["else" ":" suite]
7.3 The for statement
for_stmt ::= "for" target_list "in" expression_list ":" suite
["else" ":" suite]
7.4 The try statement
try_stmt ::= try1_stmt | try2_stmt
try1_stmt ::= "try" ":" suite
("except" [expression ["as" target]] ":" suite)+
["else" ":" suite]
["finally" ":" suite]
try2_stmt ::= "try" ":" suite
"finally" ":" suite
7.5 The with statement
with_stmt ::= "with" with_item ("," with_item)* ":" suite
with_item ::= expression ["as" target]
7.6 Function definitions
funcdef ::= [decorators] "def" funcname "(" [parameter_list] ")" ["->" expression] ":" suite
decorators ::= decorator+
decorator ::= "@" dotted_name ["(" [parameter_list [","]] ")"] NEWLINE
dotted_name ::= identifier ("." identifier)*
parameter_list ::= (defparameter ",")*
( "*" [parameter] ("," defparameter)*
[, "**" parameter]
| "**" parameter
| defparameter [","] )
parameter ::= identifier [":" expression]
defparameter ::= parameter ["=" expression]
funcname ::= identifier
7.7 Class definitions
classdef ::= [decorators] "class" classname [inheritance] ":" suite
inheritance ::= "(" [parameter_list] ")"
classname ::= identifier