I hav a class which needs to initialize 4 variables returned by a load() function. And in the future the load may return even more variable values. This is my current way of initializing the Runner class. Is there a more normal way or better way to do this?
class Runner(object): def __init__(self): self.first, self.second, self.third, self.mapping = self.load() def load(): ... return (first, second, third, mapping)
If you will have a very large number of such variables, they should not be variables but keys in a dictionary. The way you’re going, you’ll have to keep track in two separate places of what each position stands for (what’s the fifth variable, the sixth variable, etc.), and always keep them in sync — and that’s just one of the problems.
So use a dictionary, change
load() to return a dictionary as well, and it all boils down to how to pass and copy dictionaries; for which there are many concise solutions.