Is it better to use class variables or pass parameters?

Derek Basch dbasch at yahoo.com
Thu Mar 16 00:04:19 CET 2006


One more question everybody. Say you have a class that performs a
series of evaluations on several strings of peptides. Let's say for the
sake of argument:

DMCDIYLLY
FQPQNGQFI
RHPENPNLL

Heres the class:

class PeptideEvaluator:

    def evaluate(self, peptide):
        peptide_name = peptide + "Rules!"
        result1 = self.test1(peptide, peptide_name)
        result2 = self.test2(peptide, peptide_name)
        result3 = self.test3(peptide, peptide_name)

    def test1(self, peptide, peptide_name):
        f = open(peptide_name + ".txt", "w")
        f.write(peptide)
        f.close()

    def test2(self, peptide, peptide_name):
        f = open(peptide_name + ".txt", "w")
        f.write(peptide)
        f.close()

    def test3(self, peptide, peptide_name):
        f = open(peptide_name + ".txt", "w")
        f.write(peptide)
        f.close()

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.

Thanks again everyone,
Derek Basch




More information about the Python-list mailing list