Placeholders, three ways
The same idea with TypeVar (old), def first[U] and class Box[V] (3.12), and a type alias. __type_params__ shows the placeholders.
A generic function or class works for many types while keeping track of which: first([1, 2]) gives an int, first(['a']) gives a str. You declare the placeholder with TypeVar or, since Python 3.12, with the clean def f[T](x: T) and class Box[T] syntax.
Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.
Ready for more? This phase has 5 hands-on exercises with automatic checks.
Practice questions
Write the code in the editor, run it, then open the model answer to compare.
Write last[T](items: Sequence[T]) -> T and print last([1, 2, 3]) and last("hey").
Write class Pair[A, B] holding two values, with a method swap() returning a Pair[B, A], and print the swapped pair's items.
Write type Matrix = list[list[float]] and a function that returns the sum of every number in it.
Explain it without notes
What problem does a type variable solve?
What is the difference between a bound and constraints on a type variable?
Does Stack[int]() stop you pushing a string at run time? Why or why not?
How do the 3.12 generic syntax and TypeVar differ?
Explain variance: why is list[Dog] not a list[Animal], and what is the 3.12 syntax's advantage there?
Expected output
10 a 1.5
5 hi
type params of first: (U,)
type params of Box: (V,)
alias: Pair tuple[A, A]
T name: T