Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I clear all variables in the middle of a Python script?

Tags:

python

I am looking for something similar to 'clear' in Matlab: A command/function which removes all variables from the workspace, releasing them from system memory. Is there such a thing in Python?

EDIT: I want to write a script which at some point clears all the variables.

like image 608
snakile Avatar asked Aug 22 '10 23:08

snakile


People also ask

How do you clear all variables?

To clear all variables from the current workspace, use clear or clearvars . To clear all global variables, use clear global or clearvars –global . To clear a particular class, use clear myClass . To clear a particular function or script, use clear functionName .

How do you clear variable memory in Python?

Just like the del method, you can invoke the gc. collect() for clearing the memory of not just variables but for all Python objects. Thus, you can use a combination of del() and gc. collect() to clear the variable from Python memory.

How do you delete multiple variables in Python?

When working in Python, sometimes it makes sense to be able to delete a variable or delete multiple variables in your program. The Python del keyword allows us to delete objects. We can use the del keyword to delete variables, user-defined objects, lists, items within lists, and dictionaries.

How do you clear all variables in Python Spyder?

Go to the IPython console in the Spyder IDE and type %reset. It will prompt you to enter (y/n) as the variables once deleted cannot be retrieved. Type 'y' and hit enter. That's it.


1 Answers

The following sequence of commands does remove every name from the current module:

>>> import sys >>> sys.modules[__name__].__dict__.clear() 

I doubt you actually DO want to do this, because "every name" includes all built-ins, so there's not much you can do after such a total wipe-out. Remember, in Python there is really no such thing as a "variable" -- there are objects, of many kinds (including modules, functions, class, numbers, strings, ...), and there are names, bound to objects; what the sequence does is remove every name from a module (the corresponding objects go away if and only if every reference to them has just been removed).

Maybe you want to be more selective, but it's hard to guess exactly what you mean unless you want to be more specific. But, just to give an example:

>>> import sys >>> this = sys.modules[__name__] >>> for n in dir(): ...   if n[0]!='_': delattr(this, n) ...  >>> 

This sequence leaves alone names that are private or magical, including the __builtins__ special name which houses all built-in names. So, built-ins still work -- for example:

>>> dir() ['__builtins__', '__doc__', '__name__', '__package__', 'n'] >>>  

As you see, name n (the control variable in that for) also happens to stick around (as it's re-bound in the for clause every time through), so it might be better to name that control variable _, for example, to clearly show "it's special" (plus, in the interactive interpreter, name _ is re-bound anyway after every complete expression entered at the prompt, to the value of that expression, so it won't stick around for long;-).

Anyway, once you have determined exactly what it is you want to do, it's not hard to define a function for the purpose and put it in your start-up file (if you want it only in interactive sessions) or site-customize file (if you want it in every script).

like image 63
Alex Martelli Avatar answered Sep 21 '22 23:09

Alex Martelli