Comparison of two methods of Python empty dictionary
The clear in this case means emptying the contents of the dictionary in Python, not the same as deleting the dictionary.
For example:
d={' name ': 1, ' Age ': 2}
There are two ways to empty the dictionary
Method 1:
D.clear ()
Print out the value of D as {}
Method 2:
d={}
The value of print D is {}
The difference between the two:
>>> d={' name ': 1, ' Age ': 2}
>>> x = d
>>> x
{' Age ': 2, ' name ': 1}
>>> x = {}
>>> x
{}
>>> D
{' Age ': 2, ' name ': 1}
>>>
You can see that when you use x={}, the value of x itself is cleared, and the reference to D is changed, and the value of D is not changed.
>>> d={' namge ': 1, ' Age ': 2}
>>> x = d
>>> x
{' Namge ': 1, ' Age ': 2}
>>> D
{' Namge ': 1, ' Age ': 2}
>>> X.clear ()
>>> x
{}
>>> D
{}
>>>
When X=clear () clears the value in the reference, the value of D is changed when X=clear () is used, while the value of D is not changed when using x={}.