I have a class which queries a database for it properties. As you can see in the code below, I need to set a candidates property, but then set a prime property based on the candidates property. However, I want to avoid querying the database twice. What's the best way to go about this?
class Event(object):
def __init__(self):
pass
@property
def candidates(self):
# get candidates from db
return query_result
@property
def prime(self):
# get prime based on complex logic with candidates. Simplified eg:
return self.candidates[0] # hits db again :(
If I set self.prime within the candidates definition but then call prime before candidates, I will get an error. What's the best way to do this?