April 26, 2005


I’m simply blown away by this. Check out this piece of code (if you care to) —-

def factors(num):
  i = 2
  yield 1
  while i<((num/2)+1):
    if num%i == 0: 
      yield i
    i = i+1

if __name__ == "__main__":
  f = factors(12321)
  for i in f:
    print i,

Which gives you the output —

1 3 9 37 111 333 1369 4107

Generators are similar to functions, but they remember their state. And you can iterate over a generator’s output. I can already think of so many ways in which this is cool. The language is Python of course.

The Charming Python column on this is really neat. You can always Google this for more info then.

1 comment:

Sunil Natraj said...

I was bored. So stopped by.
CS sometime. Just call.