Based on this comment about reify,
It acts like @property, except that the function is only ever called once; after that, the value is cached as a regular attribute. This gives you lazy attribute creation on objects that are meant to be immutable.
I have this custom reify class:
class reify(object):
def __init__(self, wrapped):
self.wrapped = wrapped
def __get__(self, inst):
if inst is None:
return self
val = self.wrapped(inst)
setattr(inst, self.wrapped.__name__, val)
return val
And it's used like:
@reify
def user_details(self, user_id):
try:
# function that executes db query, returns dict
return user_details_from_id(self._dbconn, user_id)
except Exception as e:
pass
Clearly, we can use it just doing name = self.user_details.get("name")
.
This works as expected, but not sure if this is caching the result or executing the query every time called, how can I confirm? I mean is this implementation correct? (don't have DB console)