+ 4

What's happening here?

uncommenting the only commented line fails the code. why is it? also, without uncommenting, if i try list comprehension instead of generator, there is RecursionError. https://code.sololearn.com/cRvoK31O3qFN/?ref=app

13th May 2022, 3:25 AM
Harsha S
Harsha S - avatar
2 ответов
+ 6
In the first part there is a generator. And a generator is just a set of commands yet to be executed. A = (i for i in range(67)) Here, A is a generator. It only knows 'what to do' and hasn't done anything yet. It hasn't created the sequence of numbers. That is why this also runs without error: def evens(): #infinite sequence n = 0 while True: yield i i += 2 B = evens() Now when you execute the generator, you may get error since it is only now that the generator-code is running. For example, print(list(B)) #memoryerror here, the actual sequence is being created hence error. Another example: def recur(): yield 1 #to make it a generator yield from recur() C = recur() #no error print(list(C)) #recursionerror
13th May 2022, 4:08 AM
abhinav
abhinav - avatar
+ 3
Harsha S If n=50, then your code works on Sololearn, so I think fibonacci 500 may be too big for the allowed processing time on Sololearn. It fails above n = 498
13th May 2022, 4:00 AM
Rik Wittkopp
Rik Wittkopp - avatar