0

What is the benefit of using Generators ?

Is there any example which can prove that a particular program can be written in a simpler way by using generators ?

25th May 2018, 2:58 AM
Vikram Kumar
Vikram Kumar - avatar
1 Answer
+ 20
#Normal : class Solo: def __init__(self, max = 0): self.max = max def __iter__(self): self.n = 0 return self def __next__(self): if self.n > self.max: raise StopIteration result = 2 ** self.n self.n += 1 return result =================== #Generator : def SoloGen(max = 0): n = 0 while n < max: yield 2 ** n n += 1 #Easy 2 implement #memory efficient check out this link 👇 https://www.programiz.com/JUMP_LINK__&&__python__&&__JUMP_LINK-programming/generator
25th May 2018, 3:23 AM
🌛DT🌜
🌛DT🌜 - avatar