title: Python: automatically delete class object after return statement
body: <p>I have a class that contains a function inside which I build and store a potentially very large bumpy array which I read form an HDF5 file. (I store the array as a class object so that when I read the various HDF5 datasets from the file I can use a list comprehension rather than a manual loop.) What I would like is to return and delete the stored array simultaneously (i.e. clean up the memory after the return statement).</p>
  
  <pre><code>class MWE(object):
        def __init__(self,*args):
            pass
            return
  
        def example(self,**kwargs):
            self.array = readLargeArrayFromHDF5File(...)
            return self.array # And simultaneously clean up self.array?
  </code></pre>
  
  <p>I know that I could simply delete the array using something like <code>del MWE.array</code> later on in my script, but is there any way to automatically clean up without me having to remember to do this?</p>
  
  <p>Thanks!</p>