我想刪除所有引用一個內存位置的對象。如果我不知道他們的全名,該怎麼辦?如何刪除在Python中引用對象的內存位置?
class Foo(object):
pass
class Bar(object):
pass
a = Foo()
b = a
c = Bar()
c.a_ref = a
c.b_ref = b
for item in a, b, c.a_ref, c.b_ref:
print(id(item))
""" Out:
140035270075472
140035270075472
140035270075472
140035270075472
"""
UPD:
確定。我想刪除link
或port1.link
,不用擔心所有連接到它的其他端口(port2.link
),它們也不得不消失。
class LINK(object):
pass
class PORT(object):
def __init__(self, link=None):
self.link = link
def __repr__(self):
return str(id(self.link))
link = LINK()
port1 = PORT()
port2 = PORT()
port1.link = port2.link = link
print(id(link), port1, port2)
# (140586778512720, 140586778512720, 140586778512720)
del link
print(port1, port2)
# (140586778512720, 140586778512720)
# want: (None, None) ???
爲什麼?你真正的問題是什麼? – 2015-03-25 07:35:52
我是一個小更新的例子,也許它可以做不同,但我認爲刪除內存位置的想法是邏輯 – uralbash 2015-03-25 08:10:55
如果我的答案有幫助,不要忘記接受它。謝謝! – 2015-03-26 13:42:47