0
How dose this code works?
import random class VagueList: def __init__(self, cont): self.cont = cont def __getitem__(self, index): return self.cont[index + random.randint(-1, 1)] def __len__(self): return random.randint(0, len(self.cont)*2) vague_list = VagueList(["A", "B", "C", "D", "E"]) print(len(vague_list)) print(len(vague_list)) print(vague_list[2]) print(vague_list[2])
1 Odpowiedź
+ 1
Where do have doubt in code?
Do you have some idea any?
edit:
VagueList(["A", "B", " C", "D", "E"]) will automatically calls constructor method __init__() and assign self.cont to passed list.
len(vague_list) will automatically calls Overiden __len__() method . so return str at the index at (index+ random letter between (-1, 1 ).
valuge_list[2] calls Overiden length method and return random number between 0 to len(self.cont) *2