Here is an example of something I want to do in code:
class MyClass(object):
@classmethod
def test_func(cls, x):
try:
return cls.cache[x]
except AttributeError:
cls.cache = {}
except (AttributeError, KeyError):
cls.cache[x] = "CACHE STORE"
return cls.cache[x]
The idea here being my class will cache some results based on an input x. However I don't want to start creating the cache until it's needed. So the first time I pass any x into this, I want it to create the cache, and fill it with something. Is there a way to make it hit both except blocks? Right now it currently only hits the first
cache = {}as a class attribute and be done with it.