[Tutor] General programming question

Kent Johnson kent37 at tds.net
Tue Jul 18 15:22:21 CEST 2006


Tino Dai wrote:
> On 7/18/06, *Kent Johnson* <kent37 at tds.net <mailto:kent37 at tds.net>> 
> wrote:
>
>     Tino Dai wrote:
>     > Hi Everybody,
>     >
>     >      I have a general question about programming. My program that I
>     > have been writing is fully modularized. My question is: Is there a
>     > programming technique that would alleviate the passing of a huge
>     > number of variables. Let me further elucidate. I could see a day
>     when
>     > I would be writing a several thousand line program that the
>     variables
>     > that I would be passing into a object's method would be several
>     lines
>     > long. In writing this email, I see two solutions. One is packing
>     them
>     > into a list or a dictionary and then passing it up to the method.
>     You can also create a class to hold the variables and pass a class
>     instance to the method.
>
>
> Could you give me a snippet of code or a reference page to how you 
> would send a instance to a method
Something like this:

class Params(object):
  def __init__(self, p1, p2):
    self.p1 = p1
    self.p2 = p2

def do_something(params):
  print params.p1, params.p2

p = Params(1, 2)
do_something(p)

Here is a generic class that does pretty much the same thing:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52308
>
>     > The second I could see is passing variables into an ancestor and
>     > having that variable(s) propogate into the children, grandchildren,
>     > etc, etc so that you would be passing the set of variables once
>     > instead into the ancestor rather than to all the children.
>     I'm not sure what you mean by this, can you explain?
>
>
> Actually, on revisiting that. It doesn't work. An instance of a parent 
> doesn't have anything to do with an instance of a child variable wise. 
> AKA, if you pass in a variable into an instance of the parent, the 
> child instance doesn't see it. This just means one thing - Tino needs 
> to get to bed earlier! ;)
I still don't know what you mean my parent and child here.
>
>     I guess you are using some classes but maybe your class design doesn't
>     fit the data. Try to find clusters of functions that act on the same
>     data and put those functions into a class. Ideally your classes will
>     each be responsible for a small, well-defined bit of the overall data
>     and functionality. Each class carries the data it needs to do its
>     work.
>     Then instead of passing around a bunch of parameters you just pass
>     a few
>     objects that encapsulate the data.
>
>
>
> Thanks for that  knowledge nugget. I will be using that in my project.

You're welcome.

Kent

PS Please reply on-list.



More information about the Tutor mailing list