How dose this code works? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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])

7th Jan 2023, 9:48 AM
Imran Shakil
Imran Shakil - avatar
1 Answer
+ 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
7th Jan 2023, 10:31 AM
Jayakrishna 🇮🇳