• About
  • Archive
  • RSS

code.dump();

Python: cached generator

“A generator and a list used like a cache.”

class GeneratorList(object):

    def __init__(self, generator):
        self.__generator = generator
        self.__list = []

    def __getitem__(self, index):
        for _ in range(index - len(self.__list) + 1):
            self.__list.append(self.__generator.next())
        return self.__list[index]
  • Posted by inky
  • Tags:
    • python
  • April 25, 2009, 8:36pm

  • Permalink
← Previous Post   Next Post →

About

A place for code snippets, keyboard shortcuts, dirty hacks and other fun tricks. Use at your own risk!

Members

  • inky
  • arood
  • beret
  • nosmo

Search

Colophon

A group tumblelog, originally by inky. Powered by Tumblr and themed by Bill Israel.