I have written the following code. But I am receiving TypeError: Cannot instantiate typing.TypeVar
from typing import TypeVar, Generic, List, Tuple
T = TypeVar('T')
class MyInt:
p: int
def __init__(self, value: int):
self.p = value
class Vector(Generic[T]):
items: List[T]
def __init__(self, sz: int):
self.items = [T(0) for _ in range(sz)]
def __init__(self, l: List[T]):
self.items = l
class Matrix(Generic[T]):
items: List[Vector[T]]
def __init__(self, rw: int, cl: int):
self.items = [Vector[T](cl) for _ in range(rw)]
def __init__(self, sz: Tuple[int, int]):
rw, cl = sz
self.items = [Vector[T](cl) for _ in range(rw)]
myMatrix = Matrix[MyInt](1, 2)
I don't know how to create an instance of MyInt when I create an instance of Matrix[MyInt]. Can you help me in this?