Vous avez des améliorations (ou des corrections) à proposer pour ce document :
je vous remerçie par avance de m'en faire part, cela m'aide à améliorer le site.
Type variable tuple. A specialized form of type variable that enables
variadic generics.
The preferred way to construct a type variable tuple is via the
dedicated syntax for generic functions, classes, and type aliases,
where a single '*' indicates a type variable tuple::
def move_first_element_to_last[T, *Ts](tup: tuple[T, *Ts]) -> tuple[*Ts, T]:
return (*tup[1:], tup[0])
Type variables tuples can have default values:
type AliasWithDefault[*Ts = (str, int)] = tuple[*Ts]
For compatibility with Python 3.11 and earlier, TypeVarTuple objects
can also be created as follows::
Ts = TypeVarTuple('Ts') # Can be given any name
DefaultTs = TypeVarTuple('Ts', default=(str, int))
Just as a TypeVar (type variable) is a placeholder for a single type,
a TypeVarTuple is a placeholder for an *arbitrary* number of types. For
example, if we define a generic class using a TypeVarTuple::
class C[*Ts]: ...
Then we can parameterize that class with an arbitrary number of type
arguments::
C[int] # Fine
C[int, str] # Also fine
C[()] # Even this is fine
For more details, see PEP 646.
Note that only TypeVarTuples defined in the global scope can be
pickled.
Améliorations / Corrections
Vous avez des améliorations (ou des corrections) à proposer pour ce document : je vous remerçie par avance de m'en faire part, cela m'aide à améliorer le site.
Emplacement :
Description des améliorations :