handling many default values

pgarrone at acay.com.au pgarrone at acay.com.au
Sat Nov 11 09:07:13 EST 2006


Alan Isaac wrote:
> > At Friday 10/11/2006 14:11, Alan G Isaac wrote:
> > >class Params:
> > >      def __init__(self,**kwargs):
> > >          #set lots of default values
> > >          ...
> > >          #set the deviations from defaults
> > >          self.__dict__.update(kwargs)
> > >
> > >Is this a reasonable approach overall?
> > >(Including the last line.)
>
>
> "Gabriel Genellina" wrote in message
> news:mailman.2074.1163201795.11739.python-list at python.org...
> > I'm not sure what you want to do exactly, but a class attribute acts
> > as a default instance attribute.
>
> Yes.  I am sorry my question was not clearer.
> There are *many* parameters,
> and the list can change,
> so I want to avoid listing them all in the Param class's __init__ function,
> using the strategy above.
>
> Q1: Is this approach reasonable?
>     (This is a newbie question about unforseen hazards.)
> Q2: Is it horrible design to isolate the parameters in a separate class?
>     (Comment: currently several classes may rely on (parts of) the same
> parameter set.)
>
> Thanks,
> Alan Isaac
Hi,
 I kind of had a similar problem when I was developing a 3d OO engine.
I had many
bigger things composed of lots of little things, and a lot of code was
spent
initializing the little things in constructors.

So I just passed a reference of the bigger thing to the little thing in
its constructor, thus saving a lot
of parameter passing and variable setup and copying. In the following
example, big is passed to
little's constructor, instead of all of big's variables.

ie.
class big:
   def __init__(self):
      self.v1 = ...
      self.v2 = ...
      self.objects = []
  def assemble(self):
      self.objects.append(little(self, 1))
      self.objects.append(little(self,2))

class little:
   def __init__(self, big, n):
     self.big = big # Now little has access to v1, v2 by self.big.v1
etc
     self.n = n  # This makes it different
  def a_function(self):
     do_something(self.big.v1, self.big.v2)




More information about the Python-list mailing list