[Tutor] Best way to construct this

Alan Gauld alan.gauld at btinternet.com
Sun Sep 2 21:11:54 CEST 2007


"Sean Cronin[tutor]" <seancron+pythontutor at gmail.com> wrote

> (in case you couldn't tell :) ).  However, I can't understand why 
> you want
> to make a separate function called parse.

OK, I want to make a separate *method* called parse, not a function.
Methods are what you have inside classes. You send messages
to instances and those messages cause methods to be executed.
That's an important concept to get your head around.
It's not the same as simply functions.

Why have a separate parse method?
Three reasons:
1) It keeps the init method simple. init methods are for initialising
the state of the object, they should not normally contain any
complex code.

2) If, after creating an instance of your class and working with
it the data gets corrupted for any reason you can reinstate
the original data by calling the parse method.

3) If you should ever have reason to subclass this class having
the parse method available as a method allws you to have a
new parse algorithm (maybe using a different parser) without
having to change the rest of the code. OTOH is the parsing
is the same but the other methods change you can leave it
as is. If the parsing is in the init then you can almost guarantee
that the init will change and you will have to rewrite the parse
code in each suib class.

> different functions in the class like maxtemp, and mintemp, is to 
> allow my
> self to be able to parse only for certain data,

Thats fine, although maxtemp is an attribute not a function/method.

> instead of having one giant function that parses for everything. 
> That
> way I have the option of being able to selectively parse the data.
> It also gives me the option as you said of being able to reparse the
> data later if it became corrupted somehow.

I'm not sure how. Your original function did the parsing in the
init method and set the values of maxtemp nodede etc from the
resultant parse tree.

> Also, where should I put the value for the maxtempnode since
> you set them equal to none in the pseudo code you sent.

I initialised tem to None because you can't set their values till 
after
parsing the file. Thus you would have to set up the max etc in
the parse method.

>>class Weather:
>>    def init(self, fname):
>>        self.filename = fname
>>        self.data = None
>>        self.maxtemp = None
>>        self.maxnode = None
>
> Should I change the None to what their values should be, or should I 
> do that
> at some point later in the code?

Reading your code it looked like the values were dependent on the 
parsed file.
If you know before parsing what they are then I'd make them parameters 
of
init with defaults:

def __Iinit__(self, fname, max=42, min=0, node=???):
        self.filename = fname
        self.data = None
        self.maxtemp = max
        self.mintemp = min
        self.maxnode = node

I'm not sure enough of how your class will be used to know
which solution fits best.

However the main point is that classes are not just collections of
functions - thats what modules are for - they are supposed to be
representations of whole entities within your program - objects.
The objects have behaviours - their methods and they manage data
for you (via the methods). Thus you should think carefully about
what objects your class represents and what purpose those
objects serve (what are their responsibilities?).

HTH
-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld




More information about the Tutor mailing list