CKTIS / Python
typing.TypeVar
Collega tipo del parametro e del risultato tramite una variabile inferita dal verificatore.
Versione: Python 3.14; repository image, production runtime not attested
Sintassi
T = TypeVar("T", bound=str)Collega tipo del parametro e del risultato tramite una variabile inferita dal verificatore.
Un limite superiore può conservare il sottotipo concreto, mentre i vincoli elencati portano a uno dei tipi indicati.
In esecuzione la funzione restituisce la medesima istanza della sottoclasse di str e accetta perfino l’intero escluso staticamente.
Esempi
from typing import TypeVar
T = TypeVar("T", bound=str)
C = TypeVar("C", str, bytes)
def same(x: T) -> T:
return x
class Label(str): pass
tag = Label("x")
print(same(tag) is tag, same(4))
print(T.__bound__ is str, C.__constraints__ == (str, bytes))
Risultato
True 4 True True
In esecuzione la funzione restituisce la medesima istanza della sottoclasse di str e accetta perfino l’intero escluso staticamente.