[Tutor] question about class

spir denis.spir at free.fr
Tue Jun 9 09:12:38 CEST 2009

Le Mon, 8 Jun 2009 17:31:23 -0600,
Vincent Davis <vincent at vincentdavis.net> s'exprima ainsi:

> Accidentally sent I have added the rest
> (by the way I refrain from using the terms attribute, method,.... as I
> will likely miss use them)
> > I am reading several tutorials about classes and trying to figure out
> > how to apply it to my project. I have a working program that basically
> > matches up applicants and schools. Schools and applicants have and
> > "true" quality and an "observed" quality. Then there is an algorithm
> > that matches them up. Right now I do this all with lists which works
> > ok but I would like to try using classes.

> > Questions
> > 1, does it make seens to have a applicant and a schools class (based
> > on this brief explanation)

Based on your explanations, I don't really understand the problem you're trying to solve, nore the algorithm. Still, probably it makes sense to use Applicant and School classes for the simple reason these notions in your program represent "objects": there are single, identified, things ; as opposed to "values" (in the ordinary sense of the term) that represent qualities or information such as color, position, number, or "true" and "observed" above.
(By the way, don't use 'true', it's too misleading. Maybe 'truth' instead.)

This notion of object identity would allow you, for instance, to match an applicant to a school by letting the applicant's attribute 'matched_school' directly point to a school itself, instead of a number that indirectly represents the school.

Also, you've got a collection of schools and applicants, which probably means they will be stored in a set or a list. Once you have a type for them, it's easier to safely manipulate them in a unified manner. Even if they have only one single data attribute, I would do it: this also brings a centralised place to expose the object type's structure and behaviour.

> > 2, is it possible to have a class for the algorithm that will modify
> > values in the applicant and schools class
> for example applicant.matched = 4 and school.matched = 343 meaning
> applicant 343 is matched to school 4

No, if I understand what you mean. You won't have a separate "class" only to store actions (python is not java), but instead you should precisely attribute these as methods to the Applicant or School types.

> 3, is I have a value set in a class applicant.foo = 5 and I what to
> use a (method?) in the class to change this, what is the right way to
> do this?

Below an example:

class Point(object):
	def __init__(self, x=0,y=0):
		self.x = x
		self.y = y
	def move(self, dx,dy):
		print self,
		self.x += dx
		self.y += dy
		print "--> %s" % self
	def __str__(self):
		return "Point (%s,%s)" % (self.x,self.y)

p0 = Point() ; print p0
p = Point(9,8) ; print p
Point (0,0)
Point (9,8)
Point (9,8) --> Point (-2,19)

la vita e estrany

More information about the Tutor mailing list