Is __del__ really a destructor?

前端 未结 4 736
没有蜡笔的小新
没有蜡笔的小新 2021-02-05 14:58

I do things mostly in C++, where the destructor method is really meant for destruction of an acquired resource. Recently I started with python (which is really a fun and fantast

4条回答
  •  臣服心动
    2021-02-05 15:39

    As the other answers have already pointed out, you probably shouldn't implement __del__ in Python. If you find yourself in the situation thinking you'd really need a destructor (for example if your class wraps a resource that needs to be explicitly closed) then the Pythonic way to go is using context managers.

提交回复
热议问题