Python Workspace Variable Clear Commands

Asked 2 years ago, Updated 2 years ago, 14 views

I'm studying Python, but I opened and closed many py files Quite a few variables have been created in the workspace variable list.

In the case of matlab, if you type the clear all command, the variables generated in the work space are also cleared at once

I want to know the commands that Python can play in the interpreter window with the same function.

It is cumbersome to click all variables and remove them with the right button every time

python

2022-09-22 20:15

1 Answers

Try globals() and del.

globals() returns the global symbol table in the dictionary form. Therefore, if you delve all the global symbols in globals(), you will get the desired results. However, variables that start with _ should not be deleted on purpose, so please do the following.

all = [var for var in globals() if var[0] != "_"]
for var in all:
    del globals()[var]


2022-09-22 20:15

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.