python3 Delete all custom variables

  • 2021-10-25 07:11:25
  • OfStack

In fact, the method is very simple ~

Enter reset and select y.

Deletion is unrecoverable.

Added: del statement in Python-variable deletion

The del statement in Python deletes variables, not data


>>> a = 1       #  Variable a Assignment 
>>> b = a       #  Will a Assign a value to b
>>> del a       #  Delete a Variable 
>>> b           #  View b Value of variable 
1
>>> a           #  View a Variable 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'a' is not defined
 
#  You can see b After being assigned a value, in the a After being deleted b The value of is still in 
#  In the right a Used del Statement, then call the a Will display a Undefined 

Supplement: Delete all user-defined variables in python once


#python
#  Delete all user-defined variables 
import re
for x in dir():
    if not re.match('^__',x) and x!="re":
        exec(" ".join(("del",x)))

Related articles: