Generics — typing documentation
A list[str] isn't just a list, it's a list of strings. Types with type parameters like this are called generic types. You can define your own generic classes ...
A list[str] isn't just a list, it's a list of strings. Types with type parameters like this are called generic types. You can define your own generic classes ...
For example, Sequence[str] is a wider type than list[str] because all list objects are also Sequence objects, but the converse is not true. A subclass is narrower than a class it derives from.
When using the older syntax, the scoping rules are more subtle and complex: ... See below for the behavior when using the built-in generic syntax in Python 3.12 ...
This page is licensed under the Python Software Foundation License Version 2. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
Significant changes to the Python Type System Specification are proposed and discussed in Python Enhancement Proposals (PEPs).
Stub files should generally follow the Style Guide for Python Code (PEP 8) and the Typing Best Practices. There are a few exceptions, outlined below, that take the different structure of stub files into account and aim to create more concise files. Syntax Example¶.