code: def getlocals(back=2):
      
      import inspect
      fr = inspect.currentframe()
      try:
          while fr and back != 0:
              fr1 = fr
              fr = fr.f_back
              back -= 1
      except:
          pass
      return fr1.f_locals
docs: Get the local variables some levels back (-1 is top).