[mod_python] mod_python, django and class variables

Rory Campbell-Lange rory at campbell-lange.net
Thu Feb 22 17:40:56 EST 2007


Hi. I'm trying out the Django web framework, which I am running through
mod_python. I mention this because I am not clear precisely how Django
hooks into mod_python.

However it has become clear that we have a problem with class variables.
A simple version of our problem is depicted below. Essentially we need
to aggregate data between instances of a class, and have been using
class variables to do this. Under mod_python the class variables keep
incrementing, however, as common-sense (but alas, not foresight)
dictates.

I would be very grateful for advice on how to somehow make our class
variables local to any particular invocation of the script in which they
are accessed. 

Regards
Rory

class Part (object):
        totalgia = 0
        def __init__(self, gia):
            self.gia = gia  # gross internal area
            self.giaratio = 0
            Part.totalgia += self.gia
        def addavgbm(self):
            self.giaratio = float(self.gia)/float(Part.totalgia)
        def __repr__(self):
            return "gia: %0.1f giaratio: %0.2f" % (self.gia, self.giaratio)

    if __name__ == '__main__':
        p1 = Part(20)
        p2 = Part(30)
        for p in p1, p2:
            p.addavgbm()
            print p


More information about the Mod_python mailing list