@@ -6,8 +6,8 @@ Defining generic classes
6
6
7
7
The built-in collection classes are generic classes. Generic types
8
8
have one or more type parameters, which can be arbitrary types. For
9
- example, Dict] int, str] has the type parameters int and str, and
10
- List[int] has a type parameter int.
9
+ example, `` Dict[ int, str] `` has the type parameters `` int `` and
10
+ `` str ``, and `` List[int] `` has a type parameter `` int `` .
11
11
12
12
Programs can also define new generic classes. Here is a very simple
13
13
generic class that represents a stack:
@@ -31,10 +31,10 @@ generic class that represents a stack:
31
31
def empty (self ) -> bool :
32
32
return not self .items
33
33
34
- The Stack class can be used to represent a stack of any type:
35
- Stack[int], Stack[Tuple[int, str]], etc.
34
+ The `` Stack `` class can be used to represent a stack of any type:
35
+ `` Stack[int] ``, `` Stack[Tuple[int, str]] `` , etc.
36
36
37
- Using Stack is similar to built-in container types:
37
+ Using `` Stack `` is similar to built-in container types:
38
38
39
39
.. code-block :: python
40
40
@@ -54,31 +54,31 @@ Type inference works for user-defined generic types as well:
54
54
Generic class internals
55
55
***********************
56
56
57
- You may wonder what happens at runtime when you index Stack. Actually,
58
- indexing Stack just returns Stack:
57
+ You may wonder what happens at runtime when you index
58
+ `` Stack ``. Actually, indexing `` Stack `` just returns `` Stack `` :
59
59
60
60
>>> print (Stack)
61
61
<class '__main__.Stack'>
62
62
>>> print (Stack[int ])
63
63
<class '__main__.Stack'>
64
64
65
- Note that built-in types list, dict and so on do not support indexing
66
- in Python. This is why we have the aliases List, Dict and so on in the
67
- typing module. Indexing these aliases just gives you the target class
68
- in Python, similar to Stack:
65
+ Note that built-in types `` list ``, `` dict `` and so on do not support
66
+ indexing in Python. This is why we have the aliases `` List ``, `` Dict ``
67
+ and so on in the `` typing `` module. Indexing these aliases just gives
68
+ you the target class in Python, similar to `` Stack `` :
69
69
70
70
>>> from typing import List
71
71
>>> List[int ]
72
72
<class 'list'>
73
73
74
74
The above examples illustrate that type variables are erased at
75
- runtime when running in a Python VM . Generic Stack or list instances
76
- are just ordinary Python objects, and they have no extra runtime
77
- overhead or magic due to being generic, other than a metaclass that
78
- overloads the indexing operator. If you worry about the overhead
79
- introduced by the type indexing operation when constructing instances,
80
- you can often rewrite such code using a # type annotation, which has
81
- no runtime impact:
75
+ runtime. Generic `` Stack `` or `` list `` instances are just ordinary
76
+ Python objects, and they have no extra runtime overhead or magic due
77
+ to being generic, other than a metaclass that overloads the indexing
78
+ operator. If you worry about the overhead introduced by the type
79
+ indexing operation when constructing instances, you can usually
80
+ rewrite such code using a `` # type: `` annotation, which has no runtime
81
+ impact:
82
82
83
83
.. code-block :: python
84
84
@@ -118,7 +118,7 @@ return type is derived from the sequence item type. For example:
118
118
s = first(' foo' ) # s has type str.
119
119
n = first([1 , 2 , 3 ]) # n has type int.
120
120
121
- Note also that a single definition of a type variable (such as T
121
+ Note also that a single definition of a type variable (such as `` T ``
122
122
above) can be used in multiple generic functions or classes. In this
123
123
example we use the same type variable in two generic functions:
124
124
0 commit comments