title: Iterating through globals() dictionary
body: <p>I am (attempting) to use <code>globals()</code> in my program to iterate through all the global variables. This is how I went about it:</p>
  
  <pre><code>for k, v in globals().iteritems():
      function(k, v)
  </code></pre>
  
  <p>Of course, in doing so, I just created 2 more global variables, <code>k</code> and <code>v</code>. So I get this exception: </p>
  
  <pre><code>RuntimeError: dictionary changed size during iteration
  </code></pre>
  
  <p>And, here are my various unsuccessful attempts at solving the issue:</p>
  
  <pre><code># Attempt 1:
  
  g = globals()
  for k, v in globals().iteritems():
      function(k, v)
  
  # Attempt 2 (this one seems to work, but on closer inspection it duplicates 
  #the last item in the dictionary, because another reference is created to it):
  
  k = v = None
  for k, v in globals().iteritems():
      function(k, v)
  </code></pre>
  
  <p>I have seen posts like <a href="https://stackoverflow.com/questions/10107676/python-runtimeerror-dictionary-changed-size-during-iteration">this</a> that deal with the same exception. This is different because there is no way to assign a variable to each dictionary entry without making a variable name for it... and doing so raises an error.</p>