%reset
%reset -f
and
%reset_selective a
%reset_selective -f a
are usefull Python alternative to the Matlab command "clear all", in which "-f" means "force without asking for confirmation" and "_selective" could be used in conjunction with
who_ls
to selectively delete variables in workspace as clearly shown here https://ipython.org/ipython-doc/3/interactive/magics.html .
Now I am managing loops in which I am going to define a large number of variables, for example
for j in range(1000):
a = crazy_function1()
b = crazy_function2()
...
m = crazy_function18()
n = crazy_function19()
...
z = crazy_functionN()
and at the end of each cycle I want to delete ALL variables EXCEPT the standard variables of the Python workspace and some of the variables I introduced (in this example only m and n). This would avoid contaminations and memory burdening hence it will make the code more efficient and safe.
I saw that "who_ls" result looks like a list, hence I thought at a loop that delete all variables that are not equal to m or n
for j in range(1000):
a = crazy_function1()
b = crazy_function2()
...
m = crazy_function18()
n = crazy_function19()
...
z = crazy_functionN()
if who_ls[j] != m or who_ls[j] != n:
%reset_selective -f who_ls[j]
but it doesn't work as who_ls looks as a list but it doesn't work as a list. How would you modify the last lines of code? Is there anything like
%reset_selective -f, except variables(m, n)
?