reference.pdf

(586 KB) Pobierz
The Python Language Reference
Release3.4.0
Guido van Rossum
Fred L. Drake, Jr., editor
April 17, 2014
Python Software Foundation
Email:docs@python.org
CONTENTS
1
Introduction
3
1.1
Alternate Implementations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3
1.2
Notation
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4
2
Lexical analysis
5
2.1
Line structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
5
2.2
Other tokens
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
7
2.3
Identifiers and keywords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
8
2.4
Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
9
2.5
Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
12
2.6
Delimiters
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
12
3
Data model
15
3.1
Objects, values and types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
15
3.2
The standard type hierarchy
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
16
3.3
Special method names
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
23
4
Execution model
37
4.1
Naming and binding
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
37
4.2
Exceptions
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
38
5
The import system
41
5.1 importlib . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
41
5.2
Packages
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
41
5.3
Searching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
42
5.4
Loading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
44
5.5
The Path Based Finder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
48
5.6
Replacing the standard import system . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
50
5.7
Special considerations for __main__ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
50
5.8
Open issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
51
5.9
References
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
51
6
Expressions
53
6.1
Arithmetic conversions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
53
6.2
Atoms
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
53
6.3
Primaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
58
6.4
The power operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
61
6.5
Unary arithmetic and bitwise operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
61
6.6
Binary arithmetic operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
62
6.7
Shifting operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
62
6.8
Binary bitwise operations
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
63
6.9
Comparisons
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
63
6.10
Boolean operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
65
6.11
Conditional expressions
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
65
6.12
Lambdas
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
65
i
6.13
Expression lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
66
6.14
Evaluation order
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
66
6.15
Operator precedence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
66
7
Simple statements
69
7.1
Expression statements
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
69
7.2
Assignment statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
69
7.3
The assert statement
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
72
7.4
The pass statement
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
72
7.5
The del statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
73
7.6
The return statement
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
73
7.7
The yield statement
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
73
7.8
The raise statement
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
74
7.9
The break statement
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
75
7.10
The continue statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
75
7.11
The import statement
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
75
7.12
The global statement
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
77
7.13
The nonlocal statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
78
8
Compound statements
79
8.1
The if statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
79
8.2
The while statement
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
80
8.3
The for statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
80
8.4
The try statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
81
8.5
The with statement
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
82
8.6
Function definitions
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
83
8.7
Class definitions
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
84
9
Top-level components
87
9.1
Complete Python programs
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
87
9.2
File input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
87
9.3
Interactive input
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
87
9.4
Expression input
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
88
10 Full Grammar specification
89
A
Glossary
93
B
About these documents
103
B.1
Contributors to the Python Documentation
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
C
History and License
105
C.1
History of the software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
C.2
Terms and conditions for accessing or otherwise using Python . . . . . . . . . . . . . . . . . . . 105
C.3
Licenses and Acknowledgements for Incorporated Software
. . . . . . . . . . . . . . . . . . . . 108
D
Copyright
121
Index
123
ii
The Python Language Reference, Release 3.4.0
This reference manual describes the syntax and “core semantics” of the language. It is terse, but attempts to be
exact and complete. The semantics of non-essential built-in object types and of the built-in functions and modules
are described in library-index. For an informal introduction to the language, see tutorial-index. For C or C++
programmers, two additional manuals exist: extending-index describes the high-level picture of how to write a
Python extension module, and the c-api-index describes the interfaces available to C/C++ programmers in detail.
CONTENTS
1
1342694504.001.png
 
Zgłoś jeśli naruszono regulamin