-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflyweight.py
More file actions
24 lines (18 loc) · 724 Bytes
/
flyweight.py
File metadata and controls
24 lines (18 loc) · 724 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
from weakref import WeakValueDictionary
class Flyweight:
"""Flyweight class"""
# Could be a simple dict.
# With WeakValueDictionary garbage collection can reclaim the object
# when there are no other references to it.
_pool: WeakValueDictionary = WeakValueDictionary()
def __new__(cls, id: int | str = 0, val: str = '<default>'):
obj = cls._pool.get(str(id) + val)
if obj is None:
obj = object.__new__(Flyweight)
cls._pool[str(id) + val] = obj
obj.id, obj.val = str(id), val # type: ignore
return obj
def clear(self) -> None:
self._pool.clear()
def __repr__(self):
return f'Flyweight #{self.id} ({self.val})'