Feb-02-2022, 06:11 PM
Quote:Yield is a strange thing that I've only seen in Python. yield returns a value, but it does not exit the function. When this code (for p in prime_numbers()) askes for the next prime, execution resumes in the prime_numbers() generator immediately after the point of the yield.
I will give you that it is a strange thing, but not unusual. It exist in C# You can see the first example https://docs.microsoft.com/en-us/dotnet/...ords/yield
Quote:prime_numbers() is a generator. It returns a prime number, not a list of prime numbers.
True but yield n picks the primes from the list prime_cache? I mean yield 2, inject, if i may say, the number 2, and in prime_cache they also added 2.
They could of returned the list and illiminate yield 2. Anyway whats the point of having a function if you need an if statement to stop the generator.
If i print(prime_cache) at line 11 it will show all primes from 2 to the infinite. I was expecting only [2] and odd numbers at this stage.
Than if i print(prime_cache) at line 17 and after, i would get primes.
My question is why before line 12 i can see the primes?
Its not the first time i see something like that. The number generated by intertools get to the prime_cache list without any use of append! That's what is bugging me.
In C# if you want those numbers to get in the list you must use prime_cache.Add(n); right at line 14 otherwise no numbers are added to the list!
Maybe its the language barrier and i am not explaining myself correctly.
Thank you