| 3 | import weakref |
| 4 | |
| 5 | class Spam: |
| 6 | _spam_cache = weakref.WeakValueDictionary() |
| 7 | def __new__(cls, name): |
| 8 | if name in cls._spam_cache: |
| 9 | return cls._spam_cache[name] |
| 10 | else: |
| 11 | self = super().__new__(cls) |
| 12 | cls._spam_cache[name] = self |
| 13 | return self |
| 14 | |
| 15 | def __init__(self, name): |
| 16 | print('Initializing Spam') |
| 17 | self.name = name |
| 18 | |
| 19 | if __name__ == '__main__': |
| 20 | print("This should print 'Initializing Spam' twice") |