我正在尝试找出删除某些东西的最佳方法,最好不必编写大量代码。
在我的项目中,我正在模拟化合物 - 我通过实例将实例Element
绑定到其他实例。在化学中,键经常断裂,我希望有一个干净的方法来做到这一点。我目前的方法如下Element
Bond
# 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
我想做类似的事情
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
或者,这样的事情会很好
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
这些方法中的任何一种都比其他方法更可取,还是有其他我忽略的方法?