File tree Expand file tree Collapse file tree 1 file changed +6
-7
lines changed
Expand file tree Collapse file tree 1 file changed +6
-7
lines changed Original file line number Diff line number Diff line change @@ -6,13 +6,6 @@ code. These types are interesting in that they are parametrised by other types!
66A ``list[str] `` isn't just a list, it's a list of strings. Types with type
77parameters like this are called *generic types *.
88
9- .. note ::
10-
11- Python 3.12 introduced :pep: `695 `, which allows defining generics using
12- type parameter syntax (e.g., ``class Foo[T]: `` and ``def func[T](x: T) -> T: ``).
13- The older ``TypeVar `` and ``Generic ``-based syntax remains supported
14- for compatibility with earlier Python versions.
15-
169You can define your own generic classes that take type parameters, similar to
1710built-in types such as ``list[X] ``. Note that such user-defined generics are a
1811moderately advanced feature and you can get far without ever using them.
@@ -40,6 +33,12 @@ Here is a very simple generic class that represents a stack:
4033 def empty (self ) -> bool :
4134 return not self .items
4235
36+ .. note ::
37+
38+ The type parameter syntax (e.g., ``class Foo[T]: ``) was introduced in Python 3.12.
39+ For earlier Python versions, generic classes need to be defined using
40+ ``TypeVar `` and ``Generic ``, as shown below.
41+
4342For compatibility with older Python versions, the same class may be written as:
4443
4544.. code-block :: python
You can’t perform that action at this time.
0 commit comments