Il Sun, 06 Nov 2005 08:33:17 -0800, Brendan ha scritto: > Hi all > > I'm new to Python (and programming in general), and I can't decide what > is the most 'pythonic' way to approach a problem. Your advice would be > appreciated. > > I have a bunch of 'scans', containing the data measured from one of > several types of 'model'. Each 'model' has different values for a list > of constants I want to create 'analyses' which interpret the data from > the scans, with reference the appropriate model. So far, I have come > up with three strategies to store this information, illustrated below > (I've simplified the constant list to just two): > > 1) Store the model constants as class instances: > > class Model: > def __init__(self, numBumps, sizeOfBumps): > self.__dict__.update(locals()); del self.self > > MODEL1 = Model(1, 2) > MODEL2 = Model(3, 4) > #etc > > class Analysis: > def __init__(self, scanData, model): > #do analysis > > 2) Store the model constants as class variables: > > class MODEL1: > numBumps = 1 > sizeOfBumps = 2 > > class MODEL2: > numBumps = 3 > sizeOfBumps = 4 > > class Analysis: > #as with 1 > > 3) Store the model constants as class variables of the analysis class: > > class Model1Analysis: > numBumps = 1 > sizeOfBumps = 2 > > def __init__(self, scanData): > #do analysis > > class Model2Analysis(Model1Analysis): > numBumps = 3 > sizeOfBumps = 4 > > There may be more options, but at this point I became paralyzed with > choice. I worry about getting stuck with an unworkable structure since > I don't have the experience to decide the merits of each in advance. I > get way too frustrated about these things :) > Brendan
My vote would go to the first option, but I don't understand why you use such a complicate way to set instance attributes. I would do simply class Model: def __init__(self, numBumps, sizeOfBumps): self.numBumps = numBumps self.sizeofBumps = sizeOfBumps or, if you want to generalize the arguments of the constructor: class Model: def __init__(self, **model_attributes): for attname, attvalue in model_attributes.items(): setattr(self, attname, attvalue) As for why to choose the first option... is the one that causes you to avoid useless code repetitions and to keep the Analysis code nicely generic. Ciao ----- FB -- http://mail.python.org/mailman/listinfo/python-list