[Tutor] Design - getFoo and setFoo methods

Lloyd Kvam pythontutor@venix.com
Sat, 18 May 2002 16:47:07 -0400


Erik Price wrote:

> it gets very confusing.  What would a Real Programmer do in this situation?


Well, until we hear from a Real Programmer, here is my opinion:


The emphasis on Get/Set methods from some sources misses the mark.  The goal is
to prevent "dumb" updates of an objects data.  A Set method can check that the
new value is consistent with the other values in the object and disallow the
new value or change some of the other values to keep the object "consistent".
Now Python can use "Set methods" even when doing what looks like a direct
assignment.  Typically, you write the amount of hidden Get/Set stuff that makes
sense for your application and usage.  See the Python2.2 documentation on
property and __setattr__.

In web applications, it is not usually practical to deal with setting one
attribute at a time.  The values come back from a form.  Youe set them all
if possible, or else return the form with some kind of error response so that
the problem can be fixed and the form can be resubmitted.  Your object's Set
method will deal with all of the attributes that are grouped together on the
form.  In this case you probably won't need Set methods that deal with one
attribute at a time.

Methods are the key to using objects.  In another post Alan Gauld wrote:
	"Class definitions should be driven by behaviour not data."

You wrote about needing to produce two kinds of addresses, one with name and
one with a hyperlinked name.  I would be inclined to have methods:
	print_name(self, URL=None):
		if URL produce hyperlinked name
	print_address(self):
		prints address EXCEPT for name
and possibly:
	print_name_address(self, URL=None):
		self.print_name(URL)
		self.print_address()
HTH
-- 
Lloyd Kvam
Venix Corp.
1 Court Street, Suite 378
Lebanon, NH 03766-1358

voice: 
603-443-6155
fax: 
801-459-9582