title: Smart caching of expensive objects in Python
body: <p>I have a directory of images in order. Typically my code will be using data from a sequential subset of images (e.g. images 5-10), and the naive options for accessing these are:</p>
  
  <ol>
  <li><p>Create a wrapper object with a method that loads the image when needed and reads my data (e.g. a pixel value). This has little memory overhead but will be slow as it will need to load each image every time.</p></li>
  <li><p>Store all the images in memory. This will be fast but obviously there's a limit to how many images we can store.</p></li>
  </ol>
  
  <p>I would like to find:</p>
  
  <ul>
  <li>Some method by which I can define how to read the image corresponding to an index or a path, and then allows me to access, say <code>magic_image_collection[index]</code> without me having to worry about whether it's going to return the object in memory or read it afresh. This would ideally keep the appropriate images or the <code>n</code> most recently accessed images in memory.</li>
  </ul>