できれば多くのコードを書かなくても、何かを削除する最善の方法を見つけようとしています。
私のプロジェクトでは、化合物をシミュレートしています。インスタンスを介し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
これらの方法のいずれかが他の方法よりも好ましいですか、それとも私が見落としている他の方法がありますか?