On Tue, 18 Jul 2006, Christopher Arndt wrote:

>> 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.

One of the things we can do is to try to group related values together 
into structures.  In Python, classes act as a way to glue related data 
together.


For example, let's say we have a program that deals with drawing shapes 
like circles.  One way we could imagine doing this is to represent a 
circle as an x/y coordinate, a radius, and a color.  In this situation, 
things that work on circles will take in at least those three arguments:

########################################################
def draw_circle(x, y, radius, color):
     ...
########################################################


But another way to look at this is to say that there is a single thing 
called a Circle with three properties: center_x, center_y, radius, and 
color:

########################################################
class Circle:
     def __init__(self, x, y, r, k):
         (self.center_x, self.center_y,
          self.radius, self.color) = (x, y, r, k)
########################################################


If we have such a structure to glue those four values into a single thing, 
then our functions that deal with circles now only have to pass that one 
thing around:

########################
def draw_circle(circle):
     ...
########################

This technique is how we avoid passing so many separate parameters around: 
we group them together.  The grouping shouldn't be random, so that's where 
intelligence and asthetics comes in.


Best of wishes!
_______________________________________________
Tutor maillist  -  [email protected]
http://mail.python.org/mailman/listinfo/tutor

Reply via email to