How to clean python interpreter's environment?

Reinhold Birkenfeld reinhold-birkenfeld-nospam at wolke7.net
Fri Aug 20 14:22:57 EDT 2004


Peter Hansen wrote:
> Rafal Kleger-Rudomin wrote:
>> I'm looking for a command to reset interpreter's environment i.e. unload 
>> all modules, delete variables etc.
> 
> If you're in the interactive interpreter, you should use
> the following command: ^Z (on Windows), or ^D (Linux).
> 
> (Translation: there's no existing command that does what
> you want.  Maybe describing your reason for wanting this will
> let people suggest alternative solutions.)

What about something like this:

def clear(keep=("__builtins__", "clear")):
    keeps = {}
    for name, value in globals().iteritems():
        if name in keep: keeps[name] = value
    globals().clear()
    for name, value in keeps.iteritems():
        globals()[name] = value

Reinhold

-- 
Wenn eine Linuxdistribution so wenig brauchbare Software wie Windows
mitbrächte, wäre das bedauerlich.  Was bei Windows der Umfang eines
"kompletten Betriebssystems" ist, nennt man bei Linux eine Rescuedisk.
  -- David Kastrup in de.comp.os.unix.linux.misc



More information about the Python-list mailing list