Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clearing user-created variables in Python

I am an absolute beginner in Python. I have been using R and Matlab for data analysis for quite some time. One of the great things about these languages (or tools) is that I could run clear all in Matlab or rm(list=ls()) in R to clear the variables I created. I could then continue to experiment with my snippet of code.

I am struggling to find such a solution in Python. I researched Is there a way to delete created variables, functions, etc from the memory of the interpreter? and found that there are two ways to accomplish what I am trying to do, but in reality they are not any close.

First method:

%Reset

This command deletes all variables and imported setting in PyCharm to run interactive mode. That is, after running above command, I have to re-run

from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"

This is very annoying.

Second Method:

Another suggested method is to write a for loop and ignore functions starting with __. This also doesn't work for the reasons above. For instance, when I start my PyCharm, I run

from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"

to ensure that I am able to run a bunch of commands together just as I would do in Matlab and R (In PyCharm, the keyboard shortcut is [Alt + Shift + E; This is called as Execute Selection in Console]). I have noticed that the for loop mentioned in that Stackoverflow thread deletes some of the variables created by running above two lines of code. This means that I won't be able to execute statements by selecting them at a time. I would have to type them individually on the prompt, which is annoying. Another option would be to run above two lines of code every time I execute the for loop, which is annoying as well.

As an aside, I did some research to understand what happens after running above two lines of code. I ran dir() after running above two lines to get a list of all variables existing in my session.

The list looks like this

Out[4]: 
['In',
 'InteractiveShell',
 'Out',
 '_',
 '_2',
 '__',
 '___', 
......(continued)
 'sys']

Is there any way, I can delete user-created variables without deleting above variables? In effect, I want to replicate what rm(list=ls()) would do for me in R.

I see that there are a number of people who have asked this question outside StackOverflow such as https://github.com/spyder-ide/spyder/issues/2563.

Thanks for any help. I am using PyCharm with the latest Conda distribution (3.6).

like image 660
watchtower Avatar asked Jan 14 '18 22:01

watchtower


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 .

How do you clear a variable in Python idle?

>>> del VAR_NAME #can delete that particular variable.

How do you clear a global variable in Python?

To delete a global variable from inside a function: Use the global keyword to mark the variable as global inside of the function. Use the del statement to delete the variable.


1 Answers

Try this:

>>> x = 1
>>> x
1
>>> globals().clear()
>>> x
Traceback (most recent call last):
  File "<pyshell#5>", line 1, in <module>
    x
NameError: name 'x' is not defined

Note that the the docs don't actually say that you can safely modify the dictionary that globals() returns, nor that modifying this dictionary will have the effect you intend (or any effect at all). But if you're just doing it for convenience in the REPL, then if it works, it works.

like image 187
kaya3 Avatar answered Oct 08 '22 03:10

kaya3