Method of restoring data by _del_ in python

  • 2021-08-21 20:49:14
  • OfStack

When there is too much data storage, we will choose to clear it, but sometimes we need to retrieve some data that we deleted before. Some small partners may use different methods to complete them respectively, so the _del_ method brought by this site today can not only satisfy the data removal, but also rediscover the deleted data. Considering that everyone is in urgent need of data restoration methods, let's explain how _del_ restores data.

_del_ is called when the object is destroyed, which is often used for operations such as clearing data or restoring environment. For example, the statement of inserting database is implemented in other common methods in the class. When the object is destroyed, we need to restore the data, so the function of restoring database data can be implemented in __del__ method. __del__ is referred to as the destructor method, again similar to the destructor method in C + +.

The execution sequence of the following example can deepen everyone's understanding of it.


class Demo:
  def __init__(self):
    print(' Call __init__ Method ')
  def func(self):
    print(' This is 1 A common method ')
  def __del__(self):
    print(' Call __del__ Method ')
d = Demo()
d.func()

When d. func () is executed, the object d is not continuously referenced in any one place. At this time, the garbage collection mechanism of Python will actively recycle this object, that is, destroy d. At this time, the __del__ method is automatically called, and the running result is as follows.

Call the __init__ method

This is a common method

Call the __del__ method

Note: When del statement deletes a variable, it releases the reference of the variable to the data, instead of deleting the data directly, instead of deleting the memory address, it only deletes the reference, and the data becomes a recyclable object, and then the memory will be recycled irregularly.

You cannot define any function or variable named del:


>>> def del(a):
 File "<stdin>", line 1
  def del(a):
     ^
SyntaxError: invalid syntax

I believe that after studying _del_ in python in this article, my friends can restore the data independently.

Extension of del Usage in python

Since python is a reference and python has the GC mechanism, the del statement acts on variables, not data objects.


if __name__=='__main__': 
 a=1 #  Object  1  Be   Variable a Reference, object 1 The reference counter of is 1 
 b=a #  Object 1  Quad variable b Reference, object 1 Add the reference counter of 1 
 c=a #1 Object 1  Quad variable c Reference, object 1 Add the reference counter of 1 
 del a # Delete a variable a , lift a Right 1 Reference to  
 del b # Delete a variable b , lift b Right 1 Reference to  
 print(c) # Final variable c Still quote 1 

del deletes variables, not data.


if __name__=='__main__': 
 li=[1,2,3,4,5] # The list itself does not contain data 1,2,3,4,5 , but contains variables: li[0] li[1] li[2] li[3] li[4] 
 first=li[0] # Copy the list, and there will be no copy of the data object, but a new variable reference will be created  
 del li[0] 
 print(li) # Output [2, 3, 4, 5] 
 print(first) # Output  1 

Related articles: