Posts: 6,817
Threads: 20
Joined: Feb 2020
Feb-02-2022, 07:07 PM
(This post was last modified: Feb-02-2022, 07:07 PM by deanhystad.)
Quote: Anyway whats the point of having a function if you need an if statement to stop the generator.
First off, it is a generator, not a function. Functions return a value and they are done. Generators return a value and hold.
The point of a generator like this is you may not always know when a sequence should end. Lets say I want a sequence of prime numbers that add up to 100 or less. I don't know when my sequence should stop exactly. I can guess, but I don't know. I could write it like this:
1 2 3 4 5 6 7 8 |
primes = []
total = 0
for p in prime_numbers():
if (total : = total + p) > 100 :
break
else :
primes.append(p)
print ( sum (primes), primes)
|
Output: 100 [2, 3, 5, 7, 11, 13, 17, 19, 23]
Or if you prefer a while loop.
1 2 3 4 5 6 7 8 |
primes = []
total = 0
prime_gen = prime_numbers()
while total < 100 :
p = next (prime_gen)
if (total : = total + p) < = 100 :
primes.append(p)
print ( sum (primes), primes)
|
My example is a bit hokey, but so is the prime_numbers() generator in your example. I think this code is supposed to demonstrate using a generator and is not meant to be used as a prime number generator.
I agree that there is probably a little language barrier here. Some of your questions are a bit awkward, but I am only proficient in one language, so you are way ahead of me. This is far from "conversational English" and many native English speakers have trouble asking questions on this forum.
Posts: 201
Threads: 37
Joined: Dec 2021
Thank you snippsat very good example i'm gonna be keeping in my study book.
Dean last post was answering pretty much my questions. TY
Posts: 201
Threads: 37
Joined: Dec 2021
Quote:First off, it is a generator, not a function. Functions return a value and they are done. Generators return a value and hold.
But they still use the key word "def" and without the "yield" there is no generator. One would think they could of created some kind of method or class to represent the Generators.
Quote:This is far from "conversational English" and many native English speakers have trouble asking questions on this forum.
I would of think of non-native speakers. Anyway this forum is great. If you ask any questions on stackoverflow. If the question is not detailed or properly formulated they just kick you out after 3 questions.
French-canadian language is like Catalan with spanish not to far from french(France) and not to far from english(american).
Posts: 7,324
Threads: 123
Joined: Sep 2016
Feb-03-2022, 12:17 AM
(This post was last modified: Feb-03-2022, 12:18 AM by snippsat.)
(Feb-02-2022, 07:29 PM)Frankduc Wrote: But they still use the key word "def" and without the "yield" there is no generator. One would think they could of created some kind of method or class to represent the Generators. Can make a generator and not using a function,bye making a generator expression .
1 2 3 4 5 6 |
>>> squares = (n * * 2 for n in range ( 5 ))
>>> squares
<generator object <genexpr> at 0x0000019FB47C6510 >
>>> list (squares)
[ 0 , 1 , 4 , 9 , 16 ]
|
From outside of function there is also Iterator .
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
>>> lst = [ 1 , 2 , 3 ]
>>> it = iter (lst)
>>> it
<list_iterator object at 0x0000019FB48430A0 >
>>> next (it)
1
>>> next (it)
2
>>> next (it)
3
>>> list (it)
>>> it = iter (lst)
>>> it
<list_iterator object at 0x0000019FB47AC040 >
>>> list (it)
[ 1 , 2 , 3 ]
|
So work it similar way,in fact so a Generator a subclass of Iterator .
The main strategy of this is Lazy Evaluation.
A evaluation strategy which delays the evaluation of an expression until it's value is needed ,
and also avoids repeated evaluations.
When moved from Python 2 to 3 was made a lot stuff was rewritten to use this strategy.
1 2 3 4 5 6 |
>>> range ( 5 )
[ 0 , 1 , 2 , 3 , 4 ]
>>> zip ( range ( 5 ), range ( 5 ))
[( 0 , 0 ), ( 1 , 1 ), ( 2 , 2 ), ( 3 , 3 ), ( 4 , 4 )]
|
Python 2 return a finish made list.
1 2 3 4 5 6 7 8 9 10 |
>>> range ( 5 )
range ( 0 , 5 )
>>> list ( range ( 5 ))
[ 0 , 1 , 2 , 3 , 4 ]
>>> zip ( range ( 5 ), range ( 5 ))
< zip object at 0x0000019FB41B27C0 >
>>> list ( zip ( range ( 5 ), range ( 5 )))
[( 0 , 0 ), ( 1 , 1 ), ( 2 , 2 ), ( 3 , 3 ), ( 4 , 4 )]
|
In Python 3 it's lazy evaluated,so have to iterate over it to get result or call list() or other function like sum().
Posts: 201
Threads: 37
Joined: Dec 2021
I saw similar examples in https://www.programiz.com/python-programming/iterator
Generator are in curly brackets like a tuple but they are object that return a list.
I like the use you made of list (zip) much fater than while:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
coordinate = []
y = 0
while y < 10 :
x = 0
while x < 10 :
coordinate.append((x,y))
x + = 1
coordinate.append((x,y))
y + = 1
for x in coordinate:
print (x)
|
Thanks for the tips.
|