title: Are there memory efficiencies gained when code is wrapped in functions?
body: <p>I have been working on some code.  My usual approach is to first solve all of the pieces of  the problem, creating the loops and other pieces of code I need as I work through the problem and then if I expect to reuse the code I go back through it and group the parts of code together that I think should be grouped to create functions. </p>
  
  <p>I have just noticed that creating functions and calling them seems to be much more efficient than writing lines of code and deleting containers as I am finished with them.  </p>
  
  <p>for example:</p>
  
  <pre><code>def someFunction(aList):
      do things to aList
      that create a dictionary
      return aDict
  </code></pre>
  
  <p>seems to release more memory at the end than</p>
  
  <pre><code>&gt;&gt;do things to alist
  &gt;&gt;that create a dictionary
  &gt;&gt;del(aList)
  </code></pre>
  
  <p>Is this expected behavior? </p>
  
  <p>EDIT added example code</p>
  
  <p>When this function finishes running the PF Usage shows an increase of about 100 mb the filingsList has about 8 million lines.</p>
  
  <pre><code>def getAllCIKS(filingList):
      cikDICT=defaultdict(int)
      for filing in filingList:
          if filing.startswith('.'):
              del(filing)
              continue
          cik=filing.split('^')[0].strip()
          cikDICT[cik]+=1
          del(filing)
      ciklist=cikDICT.keys()
      ciklist.sort()
  return ciklist
  
  allCIKS=getAllCIKS(open(r'c:\filinglist.txt').readlines())
  </code></pre>
  
  <p>If I run this instead I show an increase of almost 400 mb</p>
  
  <pre><code>cikDICT=defaultdict(int)
  for filing in open(r'c:\filinglist.txt').readlines():
      if filing.startswith('.'):
          del(filing)
          continue
      cik=filing.split('^')[0].strip()
      cikDICT[cik]+=1
      del(filing)
  
  ciklist=cikDICT.keys()
  ciklist.sort()
  del(cikDICT)
  </code></pre>
  
  <p>EDIT
  I have been playing around with this some more today.  My observation and question should be refined a bit since my focus has been on the PF Usage.  Unfortunately I can only poke at this between my other tasks.  However I am starting to wonder about references versus copies.  If I create a dictionary from a list does the dictionary container hold a copy of the values that came from the list or do they hold references to the values in the list? My bet is that the values are copied instead of referenced.  </p>
  
  <p>Another thing I noticed is that items in the GC list were items from containers that were deleted.  Does that make sense?  Soo I have a list and suppose each of the items in the list was [(aTuple),anInteger,[another list]].  When I started learning about how to manipulate the gc objects and inspect them I found those objects in the gc even though the list had been forcefully deleted and even though I passed the 0,1 &amp; 2 value to the method that I don't remember to try to still delete them. </p>
  
  <p>I appreciate the insights people have been sharing.  Unfortunately I am always interested in figuring out how things work under the hood.  </p>