Generics

Type variables and generic types

Interview Relevant: Advanced typing

Generics

Code Examples

python
1from typing import TypeVar, Generic
2
3T = TypeVar('T')
4
5class Stack(Generic[T]):
6    def __init__(self) -> None:
7        self.items: List[T] = []
8    
9    def push(self, item: T) -> None:
10        self.items.append(item)

AI Tutor

Ask about the topic

Sign in Required

Please sign in to use the AI tutor

Sign In