[Tutor] question about class

Alan Gauld alan.gauld at btinternet.com
Tue Jun 9 10:08:53 CEST 2009


"Vincent Davis" <vincent at vincentdavis.net> wrote

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

OK, The algorithm, is one of the key things that will likely change
in your program. When converting procedural code to use objects
it is often the case that algorithms (ie functions) get split up to put
part of the code in one object and part in another.

Thus in your case (depending on the actual algorithm,) you may
move some testing code into the school object (to determine if the
school wants the pupil!) and/or some into the applicant object (to
determine if the pupil wants the school). Finally you might retain
an application level function that simply iterates over each pupil
finding them a school by asking each school if they will accept the
pupil. Or you might iterate over the schools finding out if the pupil
wants that school (which way is most appropriate depends on
the app!).  The point is each object only knows about part of
the solution. The ownershp of the matching process probably
stays outside - maybe in another object - who/what wants to
do the matching for example?

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

Yes, almost certainly

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

Its very bad practice to have an algorithm outside a class modifying 
attributes
classes/objects should "do it to themselves". In some languages its
common to implement a bunch of get/set methods to acces attributes and
people think this meets the "DIY" principle but it doesn't really. The 
point
is the attributes should be supporting a higher level operation and that
operation is what should change the data. Some get/set methods may
be appropriate if they are truly what the class is for, but they should be
the minority. (And in Python we generally just use direct access to
attributes rather than writing one-liner get/set methods!)

So in your case you may determine that pupil A should go to school B.
You probably want to have an enrolPupil method of school or a joinSchool
method of applicant (again depends on the perspective of the application)
That method will be responsible for adding the pupil to the list of 
enrolled
pupils and/or assigning the schoool to the pupil.

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

As mentioned above you could write a get/set method, but in Python
we usually just use direct access. But in general it's best to call a 
higher
level method of the class and let it make the change!

Remember to ask yourself what is this class's responsibilities, what
do I want it to do for me? Think about behaviours first, data second.

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/ 




More information about the Tutor mailing list