Python-Ref > Advanced topics > Iterators an generators > Laziness of generators
 
 

<-^^
Moduly
Knihovní funkce

Laziness of generators

Because generators are lazy, the may implement infinite loops without any problem.
Expand/Shrink
Zdroj: (generator3-1.py)
  1   def gen_numbers():
  2     i = 0
  3     while True: # infinite loop
  4       yield i
  5       i += 1
  6   
  7   g = gen_numbers()
  8   value = g.next()
  9   print value
 10   print "----------"
 11   print g.next()
 12   print "----------"
 13   for i in range( 10):
 14     print g.next()
 15   print "----------"
 16   print g.next()
stdout:
0
----------
1
----------
2
3
4
5
6
7
8
9
10
11
----------
12
Doba běhu: 26.6 ms
Expand/Shrink
Zdroj: (generator3-2.py)
  1   import random
  2   
  3   def gen_random_numbers():
  4     while True: # infinite loop
  5       yield random.randint( 0, 100)
  6   
  7   
  8   g = gen_random_numbers()
  9   # we need 10 random numbers
 10   for i in range( 10):
 11     print g.next()
 12   print "----------"
 13   # we need another 5
 14   for i in range( 5):
 15     print g.next()
stdout:
16
62
86
83
78
96
6
4
13
50
----------
53
92
50
92
21
Doba běhu: 26.8 ms
Expand/Shrink
Zdroj: (generator3-3.py)
  1   import random
  2   
  3   def solve_equation( startx=10):
  4     """x**3 - 2*x**2 - 10*x + 5 = 0"""
  5     x = startx
  6     while True: # infinite loop
  7       x = (2*x**2 + 10*x -5) ** (1.0/3)
  8       yield x
  9   
 10   
 11   g = solve_equation()
 12   # we make 50 iterations
 13   for i in range( 50):
 14     print g.next()
stdout:
6.65693023164
5.31563842648
4.71272255461
4.4233390223
4.27931741588
4.20623204167
4.16876097569
4.14944582336
4.13946151825
4.13429294665
4.13161530859
4.13022758259
4.12950822634
4.12913529388
4.12894194568
4.12884170076
4.12878972617
4.12876277838
4.12874880644
4.12874156223
4.12873780622
4.12873585879
4.12873484908
4.12873432556
4.12873405412
4.12873391339
4.12873384042
4.12873380258
4.12873378297
4.1287337728
4.12873376752
4.12873376479
4.12873376337
4.12873376264
4.12873376226
4.12873376206
4.12873376196
4.1287337619
4.12873376188
4.12873376186
4.12873376185
4.12873376185
4.12873376185
4.12873376185
4.12873376185
4.12873376185
4.12873376185
4.12873376185
4.12873376185
4.12873376185
Doba běhu: 28.4 ms
Expand/Shrink
Zdroj: (generator3-4.py)
  1   import random
  2   
  3   def solve_equation( startx=10):
  4     """x**3 - 2*x**2 - 10*x + 5 = 0"""
  5     x = startx
  6     while True: # infinite loop
  7       x = (2*x**2 + 10*x -5) ** (1.0/3)
  8       yield x
  9   
 10   
 11   g = solve_equation( 10)
 12   # we iterate until the matches are close together
 13   oldx = 10
 14   x = g.next()
 15   while abs( x - oldx) > 0.000001:
 16     print x
 17     oldx = x
 18     x = g.next()
stdout:
6.65693023164
5.31563842648
4.71272255461
4.4233390223
4.27931741588
4.20623204167
4.16876097569
4.14944582336
4.13946151825
4.13429294665
4.13161530859
4.13022758259
4.12950822634
4.12913529388
4.12894194568
4.12884170076
4.12878972617
4.12876277838
4.12874880644
4.12874156223
4.12873780622
4.12873585879
4.12873484908
Doba běhu: 29.6 ms