728x90
반응형
728x170
■ WeakValueDictionary 클래스를 사용해 약한 참조 딕셔너리 사용하는 방법을 보여준다.
▶ om.py
import weakref
class ObjectManager:
def __init__(self):
self.weakValueDictionary = weakref.WeakValueDictionary()
def AddObject(self, sourceObject):
sourceObjectID = id(sourceObject)
self.weakValueDictionary[sourceObjectID] = sourceObject
return sourceObjectID
def GetObject(self, objectID):
try:
return self.weakValueDictionary[objectID]
except:
return None
▶ main.py
import om
class Apple:
pass
apple1 = Apple()
apple1.Color = "빨강"
objectManager = om.ObjectManager()
redID = objectManager.AddObject(apple1)
apple2 = objectManager.GetObject(redID)
print(apple1 is apple2) # True
print(apple2.Color) # 빨강
728x90
반응형
그리드형(광고전용)
'Python > weakref' 카테고리의 다른 글
[PYTHON/WEAKREF] getweakrefcount 함수 : 약한 참조 카운트 구하기 (0) | 2022.09.24 |
---|---|
[PYTHON/WEAKREF] proxy 함수 : dict 객체를 위한 약한 참조 프록시 만들기 (0) | 2022.09.22 |
[PYTHON/WEAKREF] getweakrefs 함수 : 약한 참조 리스트 구하기 (0) | 2022.09.22 |
[PYTHON/WEAKREF] proxy 함수 : 약한 참조 프록시 만들기 (0) | 2022.09.22 |
[PYTHON/WEAKREF] ReferenceType 클래스 : 약한 참조 만들기 (0) | 2022.09.22 |
댓글을 달아 주세요