tags:

views:

136

answers:

3

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.

+3  A: 

No, you are best off restarting the interpreter

Ipython is an excellent replacement for the bundled interpreter and has the %reset command which usually works

gnibbler
By 'No' you mean I shouldn't do it or that it's impossible? If you say I shouldn't do it could you explain why?
snakile
+1: It takes less than a quarter of a second for me to `ctrl-d` then `pyt`->`tab` to get back into the interpreter. No reason to do anything fancy here.
sdolan
@sdolan But what if I want to write a script which at some point wants to clear all the variables?
snakile
@snakile: Then you should say that clearly in your question.
sdolan
@sdolan added that to my question
snakile
+4  A: 

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).

Alex Martelli
Well you could always keep a copy of the original dictionary at startup and then remove names that have been added ;)However I think this is a clear abuse of the dynamic system, and I can't think of a non pointless situation to do this in.
monoceres
You are right. I shouldn't do this. Thanks.
snakile
Haha, this is brilliant... It's surprising how much functionality you lose by doing this, and yet how much functionality you can get back by doing stuff like `int = (2).__class__`, `str = ''.__class__`, `True = (1 == 1)` etc...
Chinmay Kanchi
@monoceres, how would you name that copy and where would you hide it so it doesn't get wiped away too?-)
Alex Martelli
@snakile, you're welcome!
Alex Martelli
Well, since globals() and locals() are fixed when you start the interpreter, you _could_ delete everything except `__builtins__`, '__name__', `__doc__`, `__package__` and your `reset()` function.
Chinmay Kanchi
@Chinmay, good point, basically the same kind of tricks a hacker would use to escape the attempted sandbox of an `exec` with an empty dict w/o built-ins (you can get at all types via `object.__subclasses__()`, though binding them to their "right" names is marginally trickier;-).
Alex Martelli
The more I use Python, the more I love it. Not only is it a great language, you can also spend hours simply fiddling with it and making it do things you really shouldn't ;-)
Chinmay Kanchi
@Alex: Though importing modules is tougher than just getting to the types. `__import__` is blown away along with `__builtins__`, as are `open()`, `eval()`, `execfile()` and `exec()`. So reimporting, say, `__builtin__` to get everything back is, as far as I can see, not possible.
Chinmay Kanchi
@Chinmay, hint: in 2.6, try `exec 2+2`...
Alex Martelli
+5  A: 

Write a function. Once you leave it it all names inside disappear. It is really really pointless to do this yourself in any kind of way.

The concept is called namespace and it'S so good, it made it into the Zen of Python:

Namespaces are one honking great idea -- let's do more of those!

THC4k