python clear all
时间: 2024-01-13 21:38:40 浏览: 236
python 清空文件夹下的所有文件
To clear all variables and functions from the Python namespace, you can use the `globals()` and `locals()` functions along with the `del` statement. Here's an example:
```python
# Clear all variables
vars_to_clear = list(globals().keys()) + list(locals().keys())
for var in vars_to_clear:
if var.startswith('__') and var.endswith('__'): # ignore system variables
continue
del globals()[var]
```
Please note that this approach will only clear variables within the current scope. If you have imported modules or variables defined in other scopes, they will not be cleared using this method.
阅读全文