class methods?

zakons at objectsbydesign.com zakons at objectsbydesign.com
Thu Jan 13 13:36:07 EST 2000


After looking at the alternatives, it is clear that it is much cleaner
to just create an instance to print out the statistics, as in the
following:


class Company:

    # class-level statistics
    field_counts = { }
    company_count = 0

    # some code to read in the company fields
    def read ( self, file ):
        # <...>

        # for every name/value, get statistics
        self.do_stats ( name, value )

        # <...>

        # keep track of count of companies
        Company.company_count = Company.company_count + 1

    # record the statistics
    def do_stats ( self, name, value ):
        counts = Company.field_counts
        if not counts.has_key ( name ):
            counts [ name ] = 1
        else:
            counts [ name ] = counts [ name ] + 1

    #print out the statistics
    def print_stats ( self ):
        counts = Company.field_counts
        list = counts.keys()
        list.sort()
        for name in list:
            percent = ( 100 * counts[name] ) / Company.company_count
            print "%s, %d" % ( name, percent )


# to print out the statistics
Company().print_stats()


Of course, the question: "Is this really an instance of a Company" comes
up. Does it really matter?

It would still be interesting to know if having 'class methods' is
problematic within a Python environment or if it is just a simplifying
assumption...

Thanks for the responses,
Stuart


Sent via Deja.com http://www.deja.com/
Before you buy.



More information about the Python-list mailing list