FAQ
I want to restart the environment occasionally to default (like I
restarted pythonwin). I wrote this script. I know why it doesn't work,
cause it deletes my variable (item) on each iteration. My question is:
is it possible to do this? What other things might I try?

# Reset Pythonwin enviroment

# Variables
keep = ['__builtins__', '__doc__', '__name__', 'pywin', 'keep', 'item']

print
for item in dir():
if item not in keep:
del item

print
print dir()

Search Discussions

  • Steven Bethard at Jun 23, 2006 at 5:08 pm

    Network Ninja wrote:
    I want to restart the environment occasionally to default (like I
    restarted pythonwin). I wrote this script. I know why it doesn't work,
    cause it deletes my variable (item) on each iteration. My question is:
    is it possible to do this? What other things might I try?

    # Reset Pythonwin enviroment

    # Variables
    keep = ['__builtins__', '__doc__', '__name__', 'pywin', 'keep', 'item']

    print
    for item in dir():
    if item not in keep:
    del item
    ^^^^^^^^
    del globals()[item]

    I believe that should work.

    Note that this won't cause imported modules to be reloaded though, so I
    wouldn't rely on it too heavily. The imported modules problem can only
    really be solved by restarting the interpreter.

    STeVe
  • Scott David Daniels at Jun 23, 2006 at 7:48 pm

    Network Ninja wrote:
    I want to restart the environment occasionally to default (like I
    restarted pythonwin). I wrote this script. I know why it doesn't work,
    cause it deletes my variable (item) on each iteration. My question is:
    is it possible to do this? What other things might I try?

    keep = ['__builtins__', '__doc__', '__name__', 'pywin', 'keep', 'item']
    for item in dir():
    if item not in keep:
    del item
    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

    Note that this leaves all imported modules imported.

    --Scott David Daniels
    scott.daniels at acm.org
  • Network Ninja at Jun 23, 2006 at 10:16 pm

    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".

    However, Steven's del globals()[item] did the trick perfectly. Thanks a
    million for the replies, both of you.

    So can you explain the [] after globals()? How does that work? The help
    docs don't mention anything about that in the function.
  • Scott David Daniels at Jun 24, 2006 at 12:43 am

    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

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedJun 22, '06 at 9:36p
activeJun 24, '06 at 12:43a
posts5
users3
websitepython.org

People

Translate

site design / logo © 2022 Grokbase