ch06: update from book draft

This commit is contained in:
Luciano Ramalho
2020-02-19 00:12:02 -03:00
parent 1df34f2945
commit d63b4844f1
5 changed files with 134 additions and 0 deletions

28
06-obj-ref/cheese.py Normal file
View File

@@ -0,0 +1,28 @@
"""
>>> import weakref
>>> stock = weakref.WeakValueDictionary()
>>> catalog = [Cheese('Red Leicester'), Cheese('Tilsit'),
... Cheese('Brie'), Cheese('Parmesan')]
...
>>> for cheese in catalog:
... stock[cheese.kind] = cheese
...
>>> sorted(stock.keys())
['Brie', 'Parmesan', 'Red Leicester', 'Tilsit']
>>> del catalog
>>> sorted(stock.keys())
['Parmesan']
>>> del cheese
>>> sorted(stock.keys())
[]
"""
# tag::CHEESE_CLASS[]
class Cheese:
def __init__(self, kind):
self.kind = kind
def __repr__(self):
return 'Cheese(%r)' % self.kind
# end::CHEESE_CLASS[]