Variance describes how generic container types relate under subtyping: read-only interfaces can often be covariant, while mutable collections are generally invariant.
What It Does and Why It Matters
Variance describes how generic container types relate under subtyping: read-only interfaces can often be covariant, while mutable collections are generally invariant. A default type parameter permits omission of a generic argument when the API has a sensible fallback.
Type parameter defaults are a static typing convenience, not a runtime value conversion.
Annotations guide static tools; by default they do not validate external input or change the return value at runtime.
Step-by-Step Example
Start with the complete example below. Read the inputs and predict what the final print statement will show before you run it.
class Result[ItemType = str]:
def __init__(self, value: ItemType) -> None:
self.value = value
print(Result('ready').value)
print(Result(200).value)
How the Code Works
- The definition of Result creates reusable behavior; its body runs only when called or instantiated as appropriate.
- The print calls display the results. Compare these results with the method’s documented return type and the input used.
Expected output:
ready
200
Each displayed line comes from the corresponding calculation or check. The output helps verify the return values before you adapt the example to your own input.
What to Notice
Pay attention to the accepted input types, the returned value and the boundary behavior. Predict what changes if you replace the example input with an empty, zero, negative or missing value, where that value is meaningful for this operation.
If the task uses a library function, inspect its documented exceptions and return type before reusing it inside a larger function.
Where You Would Use This
Use this in calculations, data summaries, simulations or validation when the numerical assumptions match the task. Check input domains, units and precision before interpreting a result. A single example output does not prove a statistical conclusion.
Practice Exercise
Write a generic read-only result class with a default element type.
Before running your edited example, write down the result you expect. Then test at least one valid boundary or deliberately invalid value, using appropriate exception handling where necessary.
Reference: official Python documentation for the complete API contract and version notes.
Common Beginner Mistakes
- Assuming list[Dog] can safely be used wherever a mutable list[Animal] is expected.
- Overlooking the documented condition: variance describes how generic container types relate under subtyping: read-only interfaces can often be covariant, while mutable collections are generally invariant.
Key Takeaways
- Variance describes how generic container types relate under subtyping: read-only interfaces can often be covariant, while mutable collections are generally invariant.
- Write a generic read-only result class with a default element type.
- Annotations guide static tools; by default they do not validate external input or change the return value at runtime.