Is it better to use class variables or pass parameters?

Terry Hancock hancock at anansispaceworks.com
Thu Mar 16 06:55:30 EST 2006


On 15 Mar 2006 15:04:19 -0800
"Derek Basch" <dbasch at yahoo.com> wrote:
> 
> class PeptideEvaluator:
> 
>     def evaluate(self, peptide):
> [...]
> 
> So, you instantiate a class called "PeptideEvaluator" and
> pass in each string to its "evaluate" method. Now you have
> to repeatedly pass the peptide and peptide_name to each
> function. According to what everyone has said declaring
> them as class variables is bad because they are not
> related to the state of the "PeptideEvaluator". How can I
> avoid having to pass the same parameters all over a class?
> I can';t quite seem to wrap my head around this one.

"PeptideEvaluator" suggests it's meant to act on some
"data" object and produce a "result" object (you're
reinventing functions, using object-oriented machinery?).

A more usual OOP approach is to make an object "Peptide"
which models the peptide, then put the methods on it.
When you instantiate "Peptide" you tell it what its sequence
is, and the methods provide your analysis -- you ask the
object itself to give you the results.  Then, of course,
the peptide sequence *IS* a fundamental property of the
object.

If you have to separate this functionality into a separate
object, you should immediately be asking yourself "why"? Is
there a compelling reason you need *two* objects that have
to communicate over an interface when one will do? 
(Sometimes there are).

-- 
Terry Hancock (hancock at AnansiSpaceworks.com)
Anansi Spaceworks http://www.AnansiSpaceworks.com




More information about the Python-list mailing list