간단한 플랫폼 게임을 작성 중이며 '고스트'인스턴스를 제거 할 때 해당 인스턴스가 지속되고 가비지 수집되지 않습니다. 그것은 모든 참조를 제거하고 있지만 유령 개체가 가비지 수집되는 것을 방지하는 일종의 내부 참조를 가지고있는 것 같습니다. 특히 메서드 스위치 인 특성이 있습니다.내부 참조가 가비지 수집을 막습니다.
import weakref
weak_ghosts = weakref.WeakKeyDictionary()
class Ghost(object):
def __init__(self):
#pass
self.switch = {'eat':self.eat, 'sleep':self.sleep}
def eat(self):
pass
def sleep(self):
pass
ghost = Ghost()
weak_ghosts[ghost] = None
#ghost.switch = {} # uncomment this line and ghost is successfully removed
del ghost
print "number of ghosts =", len(weak_ghosts)
#output:
number of ghosts = 1
질문 :
- 실제로 무슨 일
다음 코드는 내 문제를 보여?
- 이 상황을 피하려면 어떻게해야합니까?
- 전환 가능한 사전 사전을 만드는 올바른 방법론을 사용하고 있습니까?
그러나 실제로 그들을 호출 할 때 명시 적으로 개체를 전달해야합니다. –