在这里覆盖 __del__() 是最好的选择吗? [英] Is overriding __del__() the best choice here?
问题描述
我正在尝试找出删除某些内容的最佳方法,最好无需编写大量代码.
在我的项目中,我正在模拟化合物 - 我将 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
不会从内存中删除任何东西!它只是删除对一个对象的引用,但对象可以有多个引用:
在最后一个 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屋!