import weakref
class Cheese:
def __init__(self, kind):
self.kind = kind
def __repr__(self):
return 'Cheese({!r})'.format(self.kind)
stock = weakref.WeakValueDictionary()
cheeses = [Cheese('Cheese A'), Cheese('Cheese B'), Cheese('Cheese C')]
for cheese in cheeses:
stock[cheese.kind] = cheese
print(sorted(stock.keys())) # ['Cheese A', 'Cheese B', 'Cheese C']
del cheeses
print(sorted(stock.keys()))
-- 문제
마지막 행의 출력값과 이유는?
-- 참고
weakref.WeakValueDictionary : value 값으로 약한 참조를 가짐
약한 참조 : 참조는 하나 참조 카운터는 올리지 않음, 즉 참조가 weak reference뿐인 객체는 가비지 콜렉터에 의해 수집되어 None이 됨
다른 대부분의 언어에서와는 다른 현상이 일어남
댓글 0