I have following classes, which I used in a IPython 3.6 notebook:
class LaborResult:
value = Value()
quantity = 0.0
class Value:
absolute = 0
relative = 0
service = 0
def add(self, anotherValue):
self.absolute = self.absolute + anotherValue.absolute
self.relative = self.relative + anotherValue.relative
self.service = self.service + anotherValue.service
def str(self):
return 'Value(abs=' + str(self.absolute) + ', rel=' + str(self.relative) + ', srv=' + str(self.service) + ')'
I found out that value.relative
is initialized to a wrong value (12 instead of 0) in some cases.
When I execute
x = Value()
x.str()
I get the correct result:
'Value(abs=0, rel=0, srv=0)'
When I change the code to
y = LaborResult()
y.value.str()
the result is
'Value(abs=0, rel=12, srv=0)'
I don't see where rel=12
came from.
Unfortunately, this error does not occur, if I remove all the other code (i. e. isolate only those two classes). What are possible causes of such behavior and how to fix it (make sure that y.value.relative == 0
in the second example)?
Update:
The error disappeared after I rewrote it as:
class Value:
def __init__(self):
self.absolute = 0.0
self.relative = 0.0
self.service = 0.0
def add(self, anotherValue):
self.absolute = self.absolute + anotherValue.absolute
self.relative = self.relative + anotherValue.relative
self.service = self.service + anotherValue.service
def str(self):
return 'Value(abs=' + str(self.absolute) + ', rel=' + str(self.relative) + ', srv=' + str(self.service) + ')'
class LaborResult:
def __init__(self):
self.value = Value()
self.quantity = 0.0