Clearing pythonwin environment

Scott David Daniels scott.daniels at acm.org
Fri Jun 23 20:43:59 EDT 2006


Network Ninja wrote:
> Scott David Daniels wrote:
>>      keep = set(['__builtins__', '__doc__', '__name__',
>>                  'pywin', 'keep', 'item', 'globs'])  # Faster to test
>>      globs = globals()
>>      for item in dir():
>>          if item not in keep:
>>              del globs[item]
>>      del globs, item
> 
> This did not work, it gave an error saying "NameError: name 'globs' is
> not defined".

I suspect that was because you did not include the last entry in the
"keep" set above.

By the way, another way to clean up:

     def cleanup(keep=set(['__builtins__', '__doc__', '__name__',
                           'pywin', 'cleanup']):
         globs = globals()
         for item in set(globs) - keep:
             del globs[item]

Then, simply call:

     cleanup()

> So can you explain the [] after globals()? How does that work? 
globals() returns a dict (or dict-like object) that reflects and
access the current global environment.  Imagine:

     globs = dict(a=1, b='2', c='iii')
     print globs
     del globs['b']
     print globs


--Scott David Daniels
scott.daniels at acm.org



More information about the Python-list mailing list