3 ms·
May be I don't know Python that well, but in any language that has generators, wouldn't it be pretty straightforward to create a generic memoizator that would c
by golergka 6y ago
May be I don't know Python that well, but in any language that has generators, wouldn't it be pretty straightforward to create a generic memoizator that would cache items of provided generator after they are requested at least once? Nothing would stop you from implementing __getitem__() on it, and the initial version of Python code would still work.
Update: googled up memoizator that almost imlements exactly what I'm talking about: http://code.activestate.com/recipes/577992-memoize-generator/ http://code.activestate.com/recipes/577992-memoize-generator...
Just add __getitem__() to it that would access __cache, and you're done.