Python Forum
When did the number got included in the list?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
When did the number got included in the list?
#7
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
Reply


Messages In This Thread
RE: When did the number got included in the list? - by Frankduc - Feb-02-2022, 06:11 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Delete strings from a list to create a new only number list Dvdscot 8 1,721 May-01-2023, 09:06 PM
Last Post: deanhystad
  find random numbers that are = to the first 2 number of a list. Frankduc 23 3,526 Apr-05-2023, 07:36 PM
Last Post: Frankduc
  The included URLconf 'scribimus.urls' does not appear to have any patterns in it. nengkya 0 1,119 Mar-03-2023, 08:29 PM
Last Post: nengkya
  TypeError: float() argument must be a string or a number, not 'list' Anldra12 2 5,038 Jul-01-2022, 01:23 PM
Last Post: deanhystad
  Split a number to list and list sum must be number sunny9495 5 2,444 Apr-28-2022, 09:32 AM
Last Post: Dexty
  Divide a number by numbers in a list. Wallen 7 8,231 Feb-12-2022, 01:51 PM
Last Post: deanhystad
  Count number of occurrences of list items in list of tuples t4keheart 1 2,449 Nov-03-2020, 05:37 AM
Last Post: deanhystad
  How do I add a number to every item in a list? john316 2 2,039 Oct-28-2020, 05:29 PM
Last Post: deanhystad
  Print the number of items in a list on ubuntu terminal buttercup 2 2,006 Jul-24-2020, 01:46 PM
Last Post: ndc85430
  Make an array of string number in a List polantas 5 3,199 May-27-2020, 07:18 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020