In this Q&A, I describe why I wish to issue (say) del a,b,c
where some of the variables do not exist. All solutions work, but they make use of globals()
because that is the prevailing namespace. That is, I run a script and then work from the console. The script is constantly changing, so hiving sections of it into functions is not practical.
However, I am finding that using globals()
is not practical when some of the objects take a lot of memory. For example, I have a list of 90+ thousand nested dictionaries read from a JSON file, which the ensuing code munges into a DataFrame. Even invoking globals()
by itself causes the Python console to freeze. From trial an error I found the point at which it doesn’t come back is about 20K records.
What is a good alternative to using globals()
if I want to apply del
to a list of variables names, only some of which exist?