a short-cut command for globals().clear() ??

Ricardo Aráoz ricaraoz at gmail.com
Tue Sep 23 15:19:11 CEST 2008


Terry Reedy wrote:
> MRAB wrote:
>>
>> How about something like this:
>>
>> def clear_workspace():
>>     keep_set = set(['__builtins__', '__doc__', '__name__',
>> 'clear_workspace'])
> 
> For 2.6/3.0, add __package__ to the list to be kept.
> 
>>     for x in globals().keys():
>>         if x not in keep_set:
>>             del globals()[x]


Or... you might have a script clearWorkspace.py :

---------------------------------------------
initGlobals = globals().keys()

def clearWorkspace() :
    for gVar in globals().keys() :
        if gVar not in initGlobals :
            del globals()[gVar]
---------------------------------------------

Which you run before doing anything else. Then you don't mind if
additions were made to the list to keep, or if you are using something
like pyCrust which has its own initial globals, or if you want to keep
some global vars of your own (in which case you run clearWorkspace AFTER
you instantiate your global vars).





More information about the Python-list mailing list