In my code I want to create a bunch of lazy variables, however, if any one of them is being queried, the Load() function as a whole should be called, which will populate all the underlying lazy variables at once.
Currently my class looks like this:
@property
def test1(self):
if (not self.__test1):
self.Load()
return self.__test1
@property
def test2(self):
if (not self.__test2):
self.Load()
return self.__test2
....
Since this looks quite ugly if being done on 30+ variables, I was wondering if there is a way to do this prettier in Python, maybe with some smart decorator?
Thank you for your replies
Loadreally the method called for all of them? IfLoadreturns the same thing each time, it's not clear why you have multiple properties. If it returns a different value, then it seems like each property is cache for what is otherwise a sequence of calls to the same property.__getattr__?)self.Load()set the values of__test1et al? Otherwise, how do they get set?self.Load()returns nothing. Instead it sets all values of the variables at once (it's basically acPickle.load()operation where it's unfeasible to load the individual parts, and much more efficient to load everything at once).