0

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! 
Tejas Thakar
  • 585
  • 5
  • 19
  • 2
    Your variables are defined as class-level variables (sometimes called "static variables" e.g. in Java), not as instance variables, that is your problem. – juanpa.arrivillaga Jul 10 '17 at 07:10
  • @juanpa.arrivillaga could you please gimme some tips ? Because i'm still confused how to approach it –  Jul 10 '17 at 07:12
  • Instance variables are accessed using ``self``. The attributes you defined in the class body will be common for all instances. You could move them into ``__init__`` – Grimmy Jul 10 '17 at 07:14
  • @Grimmy Thanks. And where should i define those variable if i defined them inside the constructor then after addition i 'll loos track of everything because `__iadd__` makes a completely new object! –  Jul 10 '17 at 07:18
  • If you need to share data across all instances use a class-level variable. If you need an instance variable, use an instance variable. Quite frankly, it isn't clear what you want. What exactly is your expected output? – juanpa.arrivillaga Jul 10 '17 at 07:22
  • @juanpa.arrivillaga Lemme explain it this way. You define a box put something in it and then you expect to get this [Box number 1, capacity 20, weight 15] for example and then you define a new box B. If you print B it is already as same as the old Box with the same weight if you could just run the code you'll see it –  Jul 10 '17 at 07:29
  • @SamFarjamirad you transfer the attributes you want to keep for the new instance. – Grimmy Jul 10 '17 at 07:31
  • 1
    @SamFarjamirad You should be *explicit*, editing the question itself, don't add elaboration in the comments. It shouldn't be difficult to give a full example. The one thing you still haven't explained *explicitly* is *exactly what output you expect*. Giving exampes is always a good idea. – juanpa.arrivillaga Jul 10 '17 at 08:20

0 Answers0