MCPcopy Create free account
hub / github.com/dabeaz/python-cookbook / Spam

Class Spam

src/8/creating_cached_instances/example3.py:5–17  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

3import weakref
4
5class 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
19if __name__ == '__main__':
20 print("This should print 'Initializing Spam' twice")

Callers 1

example3.pyFile · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected