@@ -39,6 +39,20 @@ Glossary
39
39
and loaders (in the :mod: `importlib.abc ` module). You can create your own
40
40
ABCs with the :mod: `abc ` module.
41
41
42
+ annotation
43
+ A label associated with a variable, a class
44
+ attribute or a function parameter or return value,
45
+ used by convention as a :term: `type hint `.
46
+
47
+ Annotations of local variables cannot be accessed at runtime, but
48
+ annotations of global variables, class attributes, and functions
49
+ are stored in the :attr: `__annotations__ `
50
+ special attribute of modules, classes, and functions,
51
+ respectively.
52
+
53
+ See :term: `variable annotation `, :term: `function annotation `, :pep: `484 `
54
+ and :pep: `526 `, which describe this functionality.
55
+
42
56
argument
43
57
A value passed to a :term: `function ` (or :term: `method `) when calling the
44
58
function. There are two kinds of argument:
@@ -175,6 +189,10 @@ Glossary
175
189
normally contain method definitions which operate on instances of the
176
190
class.
177
191
192
+ class variable
193
+ A variable defined in a class and intended to be modified only at
194
+ class level (i.e., not in an instance of the class).
195
+
178
196
coercion
179
197
The implicit conversion of an instance of one type to another during an
180
198
operation which involves two arguments of the same type. For example,
@@ -367,14 +385,20 @@ Glossary
367
385
and the :ref: `function ` section.
368
386
369
387
function annotation
370
- An arbitrary metadata value associated with a function parameter or return
371
- value. Its syntax is explained in section :ref: `function `. Annotations
372
- may be accessed via the :attr: `__annotations__ ` special attribute of a
373
- function object.
388
+ An :term: `annotation ` of a function parameter or return value.
389
+
390
+ Function annotations are usually used for
391
+ :term: `type hints <type hint> `: for example this function is expected to take two
392
+ :class: `int ` arguments and is also expected to have an :class: `int `
393
+ return value::
394
+
395
+ def sum_two_numbers(a: int, b: int) -> int:
396
+ return a + b
397
+
398
+ Function annotation syntax is explained in section :ref: `function `.
374
399
375
- Python itself does not assign any particular meaning to function
376
- annotations. They are intended to be interpreted by third-party libraries
377
- or tools. See :pep: `3107 `, which describes some of their potential uses.
400
+ See :term: `variable annotation ` and :pep: `484 `,
401
+ which describe this functionality.
378
402
379
403
__future__
380
404
A pseudo-module which programmers can use to enable new language features
@@ -1016,6 +1040,43 @@ Glossary
1016
1040
:attr: `~instance.__class__ ` attribute or can be retrieved with
1017
1041
``type(obj) ``.
1018
1042
1043
+ type alias
1044
+ A synonym for a type, created by assigning the type to an identifier.
1045
+
1046
+ Type aliases are useful for simplifying :term: `type hints <type hint> `.
1047
+ For example::
1048
+
1049
+ from typing import List, Tuple
1050
+
1051
+ def remove_gray_shades(
1052
+ colors: List[Tuple[int, int, int]]) -> List[Tuple[int, int, int]]:
1053
+ pass
1054
+
1055
+ could be made more readable like this::
1056
+
1057
+ from typing import List, Tuple
1058
+
1059
+ Color = Tuple[int, int, int]
1060
+
1061
+ def remove_gray_shades(colors: List[Color]) -> List[Color]:
1062
+ pass
1063
+
1064
+ See :mod: `typing ` and :pep: `484 `, which describe this functionality.
1065
+
1066
+ type hint
1067
+ An :term: `annotation ` that specifies the expected type for a variable, a class
1068
+ attribute, or a function parameter or return value.
1069
+
1070
+ Type hints are optional and are not enforced by Python but
1071
+ they are useful to static type analysis tools, and aid IDEs with code
1072
+ completion and refactoring.
1073
+
1074
+ Type hints of global variables, class attributes, and functions,
1075
+ but not local variables, can be accessed using
1076
+ :func: `typing.get_type_hints `.
1077
+
1078
+ See :mod: `typing ` and :pep: `484 `, which describe this functionality.
1079
+
1019
1080
universal newlines
1020
1081
A manner of interpreting text streams in which all of the following are
1021
1082
recognized as ending a line: the Unix end-of-line convention ``'\n' ``,
@@ -1024,16 +1085,23 @@ Glossary
1024
1085
:func: `bytes.splitlines ` for an additional use.
1025
1086
1026
1087
variable annotation
1027
- A type metadata value associated with a module global variable or
1028
- a class attribute. Its syntax is explained in section :ref: `annassign `.
1029
- Annotations are stored in the :attr: `__annotations__ ` special
1030
- attribute of a class or module object and can be accessed using
1031
- :func: `typing.get_type_hints `.
1088
+ An :term: `annotation ` of a variable or a class attribute.
1089
+
1090
+ When annotating a variable or a class attribute, assignment is optional::
1091
+
1092
+ class C:
1093
+ field: 'annotation'
1094
+
1095
+ Variable annotations are usually used for
1096
+ :term: `type hints <type hint> `: for example this variable is expected to take
1097
+ :class: `int ` values::
1098
+
1099
+ count: int = 0
1100
+
1101
+ Variable annotation syntax is explained in section :ref: `annassign `.
1032
1102
1033
- Python itself does not assign any particular meaning to variable
1034
- annotations. They are intended to be interpreted by third-party libraries
1035
- or type checking tools. See :pep: `526 `, :pep: `484 ` which describe
1036
- some of their potential uses.
1103
+ See :term: `function annotation `, :pep: `484 `
1104
+ and :pep: `526 `, which describe this functionality.
1037
1105
1038
1106
virtual environment
1039
1107
A cooperatively isolated runtime environment that allows Python users
0 commit comments