|
1 | 1 | """http://codesnipers.com/?q=python-flyweights"""
|
2 | 2 |
|
3 |
| -import weakref |
| 3 | +import weakref |
4 | 4 |
|
5 | 5 |
|
6 | 6 | class Card(object):
|
7 | 7 | """The object pool. Has builtin reference counting"""
|
8 |
| - _CardPool = weakref.WeakValueDictionary() |
| 8 | + _CardPool = weakref.WeakValueDictionary() |
9 | 9 |
|
10 | 10 | """Flyweight implementation. If the object exists in the
|
11 | 11 | pool just return it (instead of creating a new one)"""
|
12 |
| - def __new__(cls, value, suit): |
13 |
| - obj = Card._CardPool.get(value + suit, None) |
14 |
| - if not obj: |
15 |
| - obj = object.__new__(cls) |
16 |
| - Card._CardPool[value + suit] = obj |
17 |
| - obj.value, obj.suit = value, suit |
| 12 | + def __new__(cls, value, suit): |
| 13 | + obj = Card._CardPool.get(value + suit, None) |
| 14 | + if not obj: |
| 15 | + obj = object.__new__(cls) |
| 16 | + Card._CardPool[value + suit] = obj |
| 17 | + obj.value, obj.suit = value, suit |
18 | 18 | return obj
|
19 | 19 |
|
20 |
| - # def __init__(self, value, suit): |
21 |
| - # self.value, self.suit = value, suit |
| 20 | + # def __init__(self, value, suit): |
| 21 | + # self.value, self.suit = value, suit |
22 | 22 |
|
23 |
| - def __repr__(self): |
24 |
| - return "<Card: %s%s>" % (self.value, self.suit) |
| 23 | + def __repr__(self): |
| 24 | + return "<Card: %s%s>" % (self.value, self.suit) |
25 | 25 |
|
26 | 26 |
|
27 | 27 | if __name__ == '__main__':
|
|
0 commit comments