0% found this document useful (0 votes)
61 views

In Summary : Generators

Generators allow you to create iterators in Python. A generator function uses the yield keyword to return a value, unlike a regular function which returns using return. When a generator yields a value, the function state is paused and remembered. The next time the generator is called, it continues where it left off. Generator expressions are similar to list comprehensions but return a generator object instead of a list. They allow iterating over sequences in a memory efficient way.

Uploaded by

Krishna
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views

In Summary : Generators

Generators allow you to create iterators in Python. A generator function uses the yield keyword to return a value, unlike a regular function which returns using return. When a generator yields a value, the function state is paused and remembered. The next time the generator is called, it continues where it left off. Generator expressions are similar to list comprehensions but return a generator object instead of a list. They allow iterating over sequences in a memory efficient way.

Uploaded by

Krishna
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

GENERATORS

Generator functions allow you to declare a function that


behaves like an iterator.
Allow programmers to make an iterator in a fast,
easy, and clean way.
generator functions allow us to create iterators in a more simple
fashion.
Generators introduce the yield stmt similar to return because it
returns a value. The difference is that [ it saves the
state ] of the function. The next time the function is called,
execution continues from where it left off, with the ..same
variable values…. it had before yielding.

 Generator Expressions is the list comprehension


equivalent of generators. It works exactly in the same way as a
list comprehension, but the expression is surrounded with ()as
opposed to [].

In summary…
 Generators allow you to create iterators in a very pythonic
manner.
 Iterators allow lazy evaluation, only generating the next
element of an iterable object when requested. This is useful
for very large data sets.
 Iterators and generators can only be iterated over once.
 Generator Functions are better than Iterators.
 Generator Expressions are better than Iterators (for simple
cases only).

-----------------------------------------------------------------------------------------------------------------------------------

https://thepythonguru.com/python-generators/

Generators are function used to create iterators, so that it can be used in the for loop.

Creating Generators

Generators are defined similar to function but there is only one difference, we use yield keyword
to return value used for each iteration of the for loop. Let’s see an example where we are trying
to clone python’s built-in range() function.

1 def my_range(start, stop, step = 1):

2 if stop <= start:

3 raise RuntimeError("start must be smaller than stop")

4 i = start

5 while i < stop:

6 yield i

7 i += step

9 try:

10 for k in my_range(10, 50, 3):


11 print(k)

12 except RuntimeError as ex:

13 print(ex)

14 except:

15 print("Unknown error occurred")

Expected Output:

1 10

2 13

3 16

4 19

5 22

6 25

7 28

8 31

9 34

10 37

11 40

12 43

13 46

14 49

Here is how my_range() works:


In for loop my_range() function get called, it initializes values of the three
arguments( start , stop and step ) and also checks whether stop is smaller than or equal to start , if
it is not then i is assigned value of start . At this point i is 10 so while condition evaluates
to True and while loop starts executing. In next statement yield transfer control to the for loop
and assigns current value of i to variable k , inside the for loop print statement get executed, then
the control again passes to line 7 inside the function my_range() where i gets incremented. This
process keeps on repeating until i <stop .

You might also like