Local class variables? (mod_python problem)
greg
greg at cosc.canterbury.ac.nz
Thu Feb 22 18:13:53 EST 2007
Rory Campbell-Lange wrote:
> class Part (object):
> totalgia = 0
> def __init__(self, gia):
> self.gia = gia # gross internal area
> self.giaratio = 0
> Part.totalgia += self.gia
>
> if __name__ == '__main__':
> p1 = Part(20)
> p2 = Part(30)
> for p in p1, p2:
> p.addavgbm()
> print p
You need another class, such as PartGroup, to keep track of
the totalgia of a group of parts (as an instance variable, not
a class variable). Then you can create a new instance of it
in your __main__ code, e.g.
class PartGroup(object):
def __init__(self):
self.parts = []
self.totalgia = 0
class Part(object):
def __init__(self, group, gia):
self.gia = gia
group.parts.append(self)
group.gia += gia
if __name__ == "__main__":
parts = PartGroup()
p1 = Part(parts, 10)
p2 = Part(parts, 20)
print parts.totalgia
A possible variation would be not to store the totalgia at
all, but have a function or method that calculates it from
the list of parts when you need it. Whether that's better or
not will depend on how frequently you need the value.
--
Greg
>
> totalgia keeps incrementing when this code is used under mod_python.
>
> We most certainly are in 'murky waters of accidental concurrent access'.
> A life vest would be gratefully received.
>
> Kind regards
> Rory
>
>
> On 22/02/07, Rory Campbell-Lange (rory at campbell-lange.net) wrote:
>
>>We have a set of classes using static methods to retain reference
>>variables between operations. The problem is that the static variables
>>are not reset between operations when used through mod_python.
>>
>>Although it is possible to reset the class variables between invocations
>>of the system, this has the potential of 'wiping out' these variables
>>when another user is using the system.
>>
>>Is there a way of getting the equivalent of 'local class variables'? In
>>other words, a way of making 'print a' and 'print b' below provide the
>>same output?
>
>
> On 22/02/07, Piet van Oostrum (piet at cs.uu.nl) wrote:
>
>>>>>>>Rory Campbell-Lange <rory at campbell-lange.net> (RC) wrote:
>
>
>>There are several errors in your python code: quite a number of comma's
>>have to be replaced by semicolons (or newlines), and there is a spurious
>>comma.
>
>
>
> On 22/02/07, Diez B. Roggisch (deets at nospam.web.de) wrote:
>
>>Rory Campbell-Lange wrote:
>
>
>>It's very unclear what you mean here, and I'm additionally under the
>>impression that you are deep in the murky waters of accidential
>>concurrent access errors here.
>
>
>
More information about the Python-list
mailing list