exec, namespace, and reference count question

Michael Owens owens at c21bowman.com
Fri Jun 28 19:43:03 EDT 2002


Since exec() allows for passing in a dictionary as the local and global 
namespaces, shouldn't it be possible to clear out all references to (global) 
variables declared in the exec'ed code simply by deleting the namespace dict? 
This doesn't work:

namespace = {}
exec(code, namespace)
del(namespace)

There are still references to the global variables instantiated within 'code' 
here. But this does:

namespace = {}
exec(code, namespace)
for key in namespace.keys():
   del(namespace[key])

Is the this then the correct/safe way to ensure that global variables created 
within the code are cleared so that there is no refcount buildup as a result 
of running the code block through exec more than once (i.e. the same global 
variables are reinstantiated and/or have their reference counts build up)? I 
am having some class object references increase after each exec, so I am 
wondering if this is the correct way to address it.






More information about the Python-list mailing list