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"?
"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.
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.
“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.
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())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With