Python Glossary Русский

Python 3.12+astterm

ast.ParamSpec

type parameters

Node for a **P type parameter in square brackets (Python 3.12): name is a string, plus default_value since 3.13.

Syntax

ast.ParamSpec(name, default_value=None)

The stars do not go into name: for **P the name is 'P', and the parameter kind is known only from the node type. ParamSpec has no bound field, unlike TypeVar. The old spelling P = ParamSpec('P') is an Assign with a call in ast, not an ast.ParamSpec.

Examples

import ast
f = ast.parse('def deco[**P, R](fn): ...').body[0]
print([type(p).__name__ for p in f.type_params])  # → ['ParamSpec', 'TypeVar']
print(f.type_params[0].name, hasattr(f.type_params[0], 'bound'))  # → P False

See also

Open in the full glossarydocs.python.orgReport an inaccuracy