Skip to content

Commit de35f89

Browse files
committed
Refine generics note per review feedback
1 parent 3bf5bbd commit de35f89

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

docs/reference/generics.rst

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,6 @@ code. These types are interesting in that they are parametrised by other types!
66
A ``list[str]`` isn't just a list, it's a list of strings. Types with type
77
parameters 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-
169
You can define your own generic classes that take type parameters, similar to
1710
built-in types such as ``list[X]``. Note that such user-defined generics are a
1811
moderately 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+
4342
For compatibility with older Python versions, the same class may be written as:
4443

4544
.. code-block:: python

0 commit comments

Comments
 (0)