All TILs
python3·
generic TypeVar
using generic TypeVar
import random
from typing import TypeVar
T = TypeVar("T")
def get_random(items: list[T]) -> T:
return random.choice(items)
using generic TypeVar for type annotationing python code, here we expect a list of Generics (can be anything) but we are ensuring that there is some connection between the input and the output and our editor will be aware of that.
← All TILsApril 15, 2026