Use TypeVar instead of Any for type-hint exercise
tbazin opened this issue · 1 comments
tbazin commented
Hi!
Nice project :)
Was just browsing through and I saw the solution to the assignment in Module 5 here uses explicit Any annotations where using a generic would seem more appropriate and better from a pedagogical perspective (I believe explicit Any annotations should generally be avoided). But I get that generics may be considered too advanced for introductory classes.
The solution would then be:
from typing import Any, Sequence, Tuple, TypeVar
T = TypeVar('T')
def get_first_and_last(x: Sequence[T]) -> Tuple[T, T]:
"""Returns the first and last elements of `x`. `x` is
assumed to be non-empty
"""
return (x[0], x[-1])