[Tutor] Variables in workspace

bob gailer bgailer at alum.rpi.edu
Sat Oct 13 16:29:33 CEST 2007


Eli Brosh wrote:
>
> Hello
> I am working with python interactively using IDLE.
>
> Since starting, I defined some variables:
> s='string'
> a=1
> b=[1,2]
> c=1.02
>
> and so on.
>
> Now, I want to know which variables are in my workspace.
> That is, is there a command similar to "who" in MATLAB ?
> I want to call "who"
> and get the output:
> s a b c
> (a listing of all the variables I defined in the session)
>
Start IDLE, then enter dir(). That will give you the names already in 
the workspace. Then assign your variables and enter dir() again.
>
>
> Now, is there a way to clear some or all the variables in that list ?
>
Clear? Do you mean set to None or delete them?

The del statement is the way to delete variables. Since dir() gives you 
their names one needs use eval.

for varName in dir():
    eval 'del ' + varName

Of course that is overkill as it will delete __builtins__ etc, so you 
want to screen out all the names that you see in the initial dir() call.


More information about the Tutor mailing list