I'm trying to program a class called Box. It works perfectly except the moment I start making a new instance. I use __iadd__
magical function to add weights to an instance so the result of this operation is a new instance that inherits some characteristics of an old version of the instance and that's the point. But when I define a completely new object, for example, a new box, then it also inherits those instance variables of an old object clearly that ain't the point. I knew I should program a reset method but is it possible to reset the instance variables just upon making a new instance? Or am I doing something totally wrong that requires a different approach?
Thanks for your help.
class Box(object):
masess = [] # Keeps track of masses
weight = 0 # The weight of box
over = 0 # represent the rest capacity of the box
full_boxes = 0 # Number of full boxes
def __init__(self, name, capacity):
self.name = name
self.capacity = capacity
def __iadd__(self, other):
Box.over = self.capacity - other # Determines the capacity of the box after some weights are added.
if Box.over >=0:
Box.masess.append(other)
Box.weight += other
if Box.weight == self.capacity:
Box.full_boxes += 1
return Box(self.name, self.capacity)
def __str__(self):
return '[%s:%s:C=%i:I=%i]' %(self.name, str(Box.masess), self.capacity,Box.weight)
@staticmethod
def get_aantal_volle_dozen():
return Box.full_boxes
A = Box('a', 20) #A new box
A +=5 # adding something to the box
A +=8
B = Box('b', 80)
print(B) # oooops!