What is the __del__ method, How to call it?

后端 未结 5 2116
广开言路
广开言路 2020-11-22 05:28

I am reading a code. There is a class in which __del__ method is defined. I figured out that this method is used to destroy an instance of the class. However, I

5条回答
  •  Happy的楠姐
    2020-11-22 06:13

    As mentioned earlier, the __del__ functionality is somewhat unreliable. In cases where it might seem useful, consider using the __enter__ and __exit__ methods instead. This will give a behaviour similar to the with open() as f: pass syntax used for accessing files. __enter__ is automatically called when entering the scope of with, while __exit__ is automatically called when exiting it. See this question for more details.

提交回复
热议问题