Branch: master
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
33 lines (24 sloc) 1.02 KB
# Dedicated to the public domain under CC0:
from typing import Callable, Generic, Iterable, TypeVar, Union
_T = TypeVar('_T')
class DefaultList(list, Generic[_T]):
A subclass of `list` that adds default elements produced by a factory function
when an out-of-bounds element is accessed.
The factory function takes the array index as a its sole parameter.
def __init__(self, factory: Callable[[int], _T], iterable: Iterable[_T]=(), fill_length=0) -> None:
self.factory = factory
for i in range(fill_length):
def __getitem__(self, index: Union[int, slice]):
if isinstance(index, slice):
end = 0 if index.stop is None else index.stop
end = index
for i in range(len(self), end):
return super().__getitem__(index)
def __repr__(self) -> str:
return '{}({}, {})'.format(type(self).__qualname__, self.factory, super().__repr__())