[SciPy-User] [SciPy-user] code structure advise for a model

David david at silveregg.co.jp
Thu Feb 3 23:42:38 EST 2011


Hi,

On 02/04/2011 11:42 AM, mdekauwe wrote:
>
> Hi,
>
> I am translating some c++ code to python and just wanted to ask some advise
> on structure. The original has everything declared globally and nothing
> passed via function (I assume, but don't know, that this isn't just standard
> c++ practice!). So given this, I have a pretty much clean slate as I can't
> quite just copy the functions over. I was thinking something like this
>
> class Params:
>
>      def __init__(self, fname):
>          self.set_inital_condtions()
>          self.read_input_file(fname)
>
>      def set_inital_conditons(self):
>          self.some_parm = 0.0
>
>
>      def read_input_file(fname):
>
>          #read file, change initial params if specified
>

Bit hard to say without more information, but you often use less custom 
classes than in a language like C++/java to pass data around.

One rule of a thumb to decide wether you want class or just pass 
arguments through list/dict is whether you need your parameters to be 
stateful (which is a good thing to avoid everything else being equal).

As for how to use inheritence: inheritence is tricky. One rule which 
works almost all the time to decide where B should derive from A is 
whether an instance of B can be used whenever an instance of A needs to 
be used (this is called the Liskov subsitution principle if you want to 
shine in discussions). Another rule is to avoid inheritence if you can: 
you should try to use composition instead, because it is more flexible 
(design mistakes in term of inheritence are hard to fix because it 
impacts all the subclasses of a changed class).

In that aspect, from what information you gave us, I don't see why Model 
should inherit from Params.

As far as Params goes, something like the following would work equally well:

def create_params(some_param=0.0):
     data = {"some_param": some_param, "other_param": ...}
     return data

def create_param_from_file(filename):
     read file content, parse it to get data
     return create_params(some_param)

This is simpler, use common code for the parameters creation. Of course, 
it may be that your code is much more complicated, in which case a class 
may be better. In the latter case, a common idiom is

class Params(object):
     # Class method just means that from_file is a method attached
     # to the class, not instances (~ static methods in java/c++).
     @classmethod
     def from_file(cls, filename):
          read file content and parse it to get data
          return cls(some_param)

     def __init__(self, some_param=0.0):
         self.some_param = some_param

You then use is as follows:

params1 = Params(some_param=1.0)
params2 = Params.from_file("foo.txt")

Using class methods for atlernate constructions is a very common idiom 
in python.

cheers,

David



More information about the SciPy-User mailing list