News for hackers. Stuff that matters.
Re:Stop writing classes... by empthought on Sunday March 18, 2012 @09:57AM (#3719701)
Check it out...

     def fibonacci_sequence():
         a, b, c = 0, 0, 1
         while True:
             a = b; b = c; c = a + b
             yield b

     >>> fibonacci_sequence()
     <generator object fibonacci_sequence at 0x108055550>
     >>> list(itertools.islice(fibonacci_sequence(), 0, 10))
     [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
     >>>

     for n in fibonacci_sequence():
         print n
         if n > 13:
             break

     1
     1
     2
     3
     5
     8
     13
     21
     >>>
Re:Stop writing classes... by scott_s on Sunday March 18, 2012 @11:33AM (#3719884)
Yes, I had forgotten about generators, pjscott and jules provide great examples for the csv case above.