I am using Jupyter Notebook and want to delete all the variables created after a certain point. I am able to do it using a for-loop looping through dir() and comparing it to the checkpoint that I create at the cell using checkpoint = list(dir()).
My goal is to clear the environment without losing the libraries that I import in the beginning (which is where I create my checkpoint). Anything I want to retain further, I can add to the checkpoint list and delete the rest.
The issue is, I don't want to write the same for-loop again whenever I want to clear the variables. As soon as I wrap that loop in a function, it stops working. There is no error; but there is no effect either.
Loop:
for i in list(dir()):
if i not in checkpoint:
exec('del {}'.format(i))
The same loop wrapped in a function:
def clear_variables():
for i in list(dir()):
if i not in checkpoint:
exec('del {}'.format(i))
The loop works. The function doesn't.