yet another generator implementation (was Re: how to write function that returns function)

George Demmy gdemmy at layton-graphics.com
Wed May 15 14:22:26 EDT 2002


Playing around a bit...

CL:

(defun foo (n) #'(lambda () (incf n)))

Python:

def foo(n=0):
  class bar:
    def __init__(self):
      self.n = n
    def next(self):
      self.n += 1
      return self.n
  return bar().next

f = foo()
print f(),f(),f()
-> 2 3 4
g = foo(1)
-> 3 4 5

Arc: ? ;)


G



More information about the Python-list mailing list