在这里覆盖 __del__() 是最好的选择吗? [英] Is overriding __del__() the best choice here?

查看:67
本文介绍了在这里覆盖 __del__() 是最好的选择吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找出删除某些内容的最佳方法,最好无需编写大量代码.

在我的项目中,我正在模拟化合物 - 我将 Element 实例通过 Bond 实例绑定到其他 Element 实例.在化学键中经常会断裂,我想有一种干净的方法来做到这一点.我目前的方法是这样的

# aBond 是一些 Bond 实例## 所有 Element 实例都有一个包含它们所属的所有键的bondList"# 他们还有一个方法 'removeBond(someBond)' 可以删除给定的债券# 从那个bondListelement1.removeBond(aBond)element2.removeBond(aBond)德尔邦德

我想做类似的事情

aBond.breakBond()类债券():def breakBond(self):self.start.removeBond(self) # 指的是 Bond 的第一部分self.end.removeBond(self) # 指的是Bond的第二部分自我

或者,这样的事情会很好

del aBond类债券():def __del__(self):self.start.removeBond(self) # 指的是 Bond 的第一部分self.end.removeBond(self) # 指的是Bond的第二部分自我

这些方法中的任何一种是否比其他方法更可取,或者是否还有其他一些我忽略的方法?

解决方案

Python 使用垃圾收集来管理内存,这意味着您不必删除任何内容.这门课很好:

class Bond():def breakBond(self):self.start.removeBond(self)self.end.removeBond(self)

注意del不会从内存中删除任何东西!它只是删除对一个对象的引用,但对象可以有多个引用:

<预><代码>>>>some_list = [1,2,3]>>>b = some_list>>>del b # 销毁列表?>>>乙回溯(最近一次调用最后一次):文件<stdin>",第 1 行,在 <module> 中NameError: 名称 'b' 未定义>>>some_list # 列表还在![1, 2, 3]>>>c = some_list>>>del some_list>>>c#列表还在![1, 2, 3]>>>德尔克

在最后一个 del c 之后,解释器可以释放列表.在 CPython 中,释放会立即完成(在这种简单的情况下),但是在该语言的其他实现中,解释器可能不会立即释放列表.

另请注意__del__ 的文档引用了这一事实.此外,它是一种真正低级方法,您在 99.9% 的情况下都不需要它,因此它肯定不是处理您的情况的正确方法.

I am trying to figure out the best way to remove something, preferably without having to write in a lot of code.

In my project I am simulating chemical compounds - I have Element instances bonded to other Element instances via a Bond instance. In chemistry bonds often break, and I'd like to have a clean way to do that. My current method is something like as follows

# aBond is some Bond instance
#
# all Element instances have a 'bondList' of all bonds they are part of
# they also have a method 'removeBond(someBond)' that removes a given bond 
# from that bondList
element1.removeBond(aBond)
element2.removeBond(aBond)
del aBond

I want to do something like

aBond.breakBond()

class Bond():
    def breakBond(self):
        self.start.removeBond(self) # refers to the first part of the Bond 
        self.end.removeBond(self) # refers to the second part of the Bond 
        del self

Alternately, something like this would be fine

del aBond

class Bond():
    def __del__(self):
        self.start.removeBond(self) # refers to the first part of the Bond 
        self.end.removeBond(self) # refers to the second part of the Bond 
        del self

Is any one of these ways of doing it preferable to the others, or is there some other way of doing this that I'm overlooking?

解决方案

Python uses garbage collection to manager memory, which means you do not have to delete anything. This class is fine:

class Bond():
    def breakBond(self):
        self.start.removeBond(self)
        self.end.removeBond(self)

note that del does not delete anything from memory! It simply removes a reference to an object, but objects can have more than one reference:

>>> some_list = [1,2,3]
>>> b = some_list
>>> del b   # destroys the list?
>>> b
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'b' is not defined
>>> some_list   # list is still there!
[1, 2, 3]
>>> c = some_list
>>> del some_list
>>> c        # list is still there!
[1, 2, 3]
>>> del c

After the last del c the interpreter can deallocate the list. In CPython the deallocation will be done immediately (in this simple case), however in other implementation of the language the interpreter might not deallocate the list immediately.

Also note that __del__'s documentation cites this fact. Furthermore it is a really low-level method which you don't need 99.9% of the time, so it certainly isn't the right way to handle your situation.

这篇关于在这里覆盖 __del__() 是最好的选择吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆