Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Infinite yield problem

Here is my simple code

class Fibonacci:
    @staticmethod
    def series():
        fprev = 1
        fnext = 1
        yield fnext
        while True:
            yield fnext
            fprev,fnext = fnext,fprev+fnext

under10 = (i for i in Fibonacci.series() if i<10)
for i in under10 :
    print i

It's absolutely obvious, but...WHY interpreter is executing block

while True:
                yield fnext
                fprev,fnext = fnext,fprev+fnext

Forever? I specified in generator,that I want only elements<10

under10 = (i for i in Fibonacci.series() if i<10)

IMHO, it's a little bit misunderstanding Any way to prevent infinite execution without re-writing "series"?

like image 601
illegal-immigrant Avatar asked Nov 01 '10 17:11

illegal-immigrant


People also ask

What does infinite yield possible mean?

"Infinite yield possible" is a warning. It lets you know that your code is unsafe, and that when this code executed, the Values object didn't exist (or was spelled incorrectly) for more than 5 seconds, and player:WaitForChild('Values') could not resolve to an object.

How do you fix infinite yield Roblox?

You can just change it to ReplicatedStorage. Infinite yield mean your WaitForChild will infinity wait when he not find the object. If you add a time, it will render nil so there will be no warn and you can replay the code if needed.

What is an infinite yield Roblox?

“Infinite yield possible” means Roblox Lua :WaitForChild() is waiting for something to appear that doesn't exist yet. It keeps checking and checking and checking, and after a while it says that in console to warn you that it could possibly be waiting forever for something to appear that will never exist.


1 Answers

How should the interpreter know that all future numbers will be < 10? It would have to either know (somehow) that it’s churning out the Fibonacci series, or it would have to inspect the whole series.

It can’t do the first, so it does the second.

You can fix this by using itertools.takewhile:

import itertools

under10 = itertools.takewhile(lambda n: n < 10, Fibonacci.series())
like image 64
Konrad Rudolph Avatar answered Oct 02 '22 00:10

Konrad Rudolph