Destroying all instances (references)
Emile van Sebille
emile at fenx.com
Sun Oct 28 09:57:26 EST 2001
----- Original Message -----
From: "Andreas Zieringer" <topdog at web.de>
Newsgroups: comp.lang.python
Sent: Sunday, October 28, 2001 3:08 AM
Subject: Destroying all instances (references)
Hi,
is it possible to destroy all created instances, something like an "new"
command in basic? I
know re-creating the interpeter would do this, but in the new interpreter I
have to import
all modules again and I can't do this. Is there an easy way doing this in
pythom or with the c-api? I couldn't
find anything in the python docu.
Regards
Andreas
This will clear all objects in __main__ that don't start with an underscore:
import __builtin__
def _init(_type='vars'):
import sys, types
main = sys.modules['__main__'].__dict__
for i in main.keys():
if not i.startswith('_'):
if _type == 'all':
del main[i]
elif _type == 'vars':
if type(main[i]) != types.ModuleType:
del main[i]
__builtin__.init = _init
If you create/add this to lib/sitecustomize.py then you will be able to
invoke init() to clear the references.
F:\Python22>python
Python 2.2b1 (#25, Oct 19 2001, 11:44:52) [MSC 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import os, sys
>>> a,b,c = 1,2,3
>>> class C:pass
...
>>> c1,c2 = C(), C()
>>> dir
<built-in function dir>
>>> dir()
['C', '__builtins__', '__doc__', '__name__', 'a', 'b', 'c', 'c1', 'c2',
'os', 'sys']
>>> init()
>>> dir()
['__builtins__', '__doc__', '__name__', 'os', 'sys']
>>> init('all')
>>> dir()
['__builtins__', '__doc__', '__name__']
>>>
HTH,
Emile van Sebille
emile at fenx.com
---------
More information about the Python-list
mailing list