| 37 | _pool: weakref.WeakValueDictionary = weakref.WeakValueDictionary() |
| 38 | |
| 39 | def __new__(cls, value: str, suit: str): |
| 40 | # If the object exists in the pool - just return it |
| 41 | obj = cls._pool.get(value + suit) |
| 42 | # otherwise - create new one (and add it to the pool) |
| 43 | if obj is None: |
| 44 | obj = object.__new__(Card) |
| 45 | cls._pool[value + suit] = obj |
| 46 | # This row does the part we usually see in `__init__` |
| 47 | obj.value, obj.suit = value, suit |
| 48 | return obj |
| 49 | |
| 50 | # If you uncomment `__init__` and comment-out `__new__` - |
| 51 | # Card becomes normal (non-flyweight). |