The Python Language Reference Release 3.9.7
Total Page:16
File Type:pdf, Size:1020Kb
The Python Language Reference Release 3.9.7 Guido van Rossum and the Python development team August 30, 2021 Python Software Foundation Email: [email protected] CONTENTS 1 Introduction 3 1.1 Alternate Implementations ...................................... 3 1.2 Notation ............................................... 4 2 Lexical analysis 5 2.1 Line structure ............................................. 5 2.1.1 Logical lines ......................................... 5 2.1.2 Physical lines ........................................ 5 2.1.3 Comments .......................................... 5 2.1.4 Encoding declarations .................................... 6 2.1.5 Explicit line joining ..................................... 6 2.1.6 Implicit line joining ..................................... 6 2.1.7 Blank lines .......................................... 7 2.1.8 Indentation ......................................... 7 2.1.9 Whitespace between tokens ................................. 8 2.2 Other tokens ............................................. 8 2.3 Identifiers and keywords ....................................... 8 2.3.1 Keywords .......................................... 9 2.3.2 Reserved classes of identifiers ................................ 9 2.4 Literals ................................................ 9 2.4.1 String and Bytes literals ................................... 10 2.4.2 String literal concatenation ................................. 12 2.4.3 Formatted string literals ................................... 12 2.4.4 Numeric literals ....................................... 14 2.4.5 Integer literals ........................................ 14 2.4.6 Floating point literals .................................... 15 2.4.7 Imaginary literals ...................................... 15 2.5 Operators ............................................... 15 2.6 Delimiters ............................................... 15 3 Data model 17 3.1 Objects, values and types ....................................... 17 3.2 The standard type hierarchy ..................................... 18 3.3 Special method names ........................................ 26 3.3.1 Basic customization ..................................... 26 3.3.2 Customizing attribute access ................................ 29 3.3.3 Customizing class creation ................................. 33 3.3.4 Customizing instance and subclass checks .......................... 36 3.3.5 Emulating generic types ................................... 36 3.3.6 Emulating callable objects .................................. 37 3.3.7 Emulating container types .................................. 37 3.3.8 Emulating numeric types .................................. 39 3.3.9 With Statement Context Managers ............................. 41 3.3.10 Special method lookup ................................... 41 i 3.4 Coroutines .............................................. 42 3.4.1 Awaitable Objects ...................................... 42 3.4.2 Coroutine Objects ...................................... 43 3.4.3 Asynchronous Iterators ................................... 43 3.4.4 Asynchronous Context Managers .............................. 44 4 Execution model 45 4.1 Structure of a program ........................................ 45 4.2 Naming and binding ......................................... 45 4.2.1 Binding of names ...................................... 45 4.2.2 Resolution of names ..................................... 46 4.2.3 Builtins and restricted execution ............................... 46 4.2.4 Interaction with dynamic features .............................. 47 4.3 Exceptions .............................................. 47 5 The import system 49 5.1 importlib ............................................. 49 5.2 Packages ............................................... 50 5.2.1 Regular packages ...................................... 50 5.2.2 Namespace packages .................................... 50 5.3 Searching ............................................... 51 5.3.1 The module cache ...................................... 51 5.3.2 Finders and loaders ..................................... 51 5.3.3 Import hooks ........................................ 52 5.3.4 The meta path ........................................ 52 5.4 Loading ................................................ 53 5.4.1 Loaders ........................................... 54 5.4.2 Submodules ......................................... 54 5.4.3 Module spec ......................................... 55 5.4.4 Import-related module attributes .............................. 55 5.4.5 module.__path__ ...................................... 56 5.4.6 Module reprs ........................................ 56 5.4.7 Cached bytecode invalidation ................................ 57 5.5 The Path Based Finder ........................................ 57 5.5.1 Path entry finders ...................................... 58 5.5.2 Path entry finder protocol .................................. 59 5.6 Replacing the standard import system ................................ 59 5.7 Package Relative Imports ....................................... 59 5.8 Special considerations for __main__ ................................. 60 5.8.1 __main__.__spec__ ..................................... 60 5.9 Open issues .............................................. 61 5.10 References .............................................. 61 6 Expressions 63 6.1 Arithmetic conversions ........................................ 63 6.2 Atoms ................................................. 63 6.2.1 Identifiers (Names) ..................................... 64 6.2.2 Literals ........................................... 64 6.2.3 Parenthesized forms ..................................... 64 6.2.4 Displays for lists, sets and dictionaries ............................ 65 6.2.5 List displays ......................................... 65 6.2.6 Set displays ......................................... 66 6.2.7 Dictionary displays ..................................... 66 6.2.8 Generator expressions .................................... 66 6.2.9 Yield expressions ...................................... 67 6.3 Primaries ............................................... 71 6.3.1 Attribute references ..................................... 71 6.3.2 Subscriptions ........................................ 71 6.3.3 Slicings ........................................... 72 ii 6.3.4 Calls ............................................. 72 6.4 Await expression ........................................... 74 6.5 The power operator .......................................... 74 6.6 Unary arithmetic and bitwise operations ............................... 74 6.7 Binary arithmetic operations ..................................... 75 6.8 Shifting operations .......................................... 76 6.9 Binary bitwise operations ....................................... 76 6.10 Comparisons ............................................. 76 6.10.1 Value comparisons ..................................... 77 6.10.2 Membership test operations ................................. 79 6.10.3 Identity comparisons .................................... 79 6.11 Boolean operations .......................................... 79 6.12 Assignment expressions ........................................ 80 6.13 Conditional expressions ........................................ 80 6.14 Lambdas ............................................... 80 6.15 Expression lists ............................................ 81 6.16 Evaluation order ........................................... 81 6.17 Operator precedence ......................................... 81 7 Simple statements 83 7.1 Expression statements ........................................ 83 7.2 Assignment statements ........................................ 84 7.2.1 Augmented assignment statements ............................. 86 7.2.2 Annotated assignment statements .............................. 86 7.3 The assert statement ........................................ 87 7.4 The pass statement ......................................... 87 7.5 The del statement .......................................... 87 7.6 The return statement ........................................ 88 7.7 The yield statement ........................................ 88 7.8 The raise statement ........................................ 89 7.9 The break statement ........................................ 90 7.10 The continue statement ...................................... 90 7.11 The import statement ........................................ 90 7.11.1 Future statements ...................................... 92 7.12 The global statement ........................................ 93 7.13 The nonlocal statement ...................................... 93 8 Compound statements 95 8.1 The if statement ........................................... 96 8.2 The while statement ........................................ 96 8.3 The for statement .......................................... 96 8.4 The try statement .......................................... 97 8.5 The with statement ......................................... 99 8.6 Function definitions .......................................... 100 8.7 Class definitions ............................................ 102 8.8 Coroutines .............................................. 103 8.8.1 Coroutine function definition ................................ 103 8.8.2 The async for statement ................................ 103 8.8.3 The async with