0

I created a dictionary of objects. I'm trying to update the class variable (numbers) by removing an element from it. The problem is that it updates every class variable of the remaining objects. If I update the variable with a single integer it does fine. Does anyone have an idea why it happens and how could I fix it?

class Matrix():
    numbers = set(range(1,10))
    def __init__(self):
        pass

def gen():
    entries = set(range(1,10))
    integer = random.randint(1,9)

    dict = {}
    for i in range(0,10):
        dict['A{0}'.format(i)] = Matrix()

    dict['A0'].numbers.remove(integer)

gen()
Rick
  • 43,029
  • 15
  • 76
  • 119
  • The `numbers` is class variable instead of instance variable. – stamaimer Jul 27 '17 at 01:54
  • "The problem is that it updates every class variable of the remaining objects." That's what a class variable (correctly called "class attribute") is- it is common to every object in the class. You need to turn your attribute into an instance attribute. – Rick Jul 27 '17 at 01:54
  • Also: see [this answer](https://stackoverflow.com/questions/68645/static-class-variables-in-python/27568860#27568860) for help with class/instance attributes. – Rick Jul 27 '17 at 01:56

0 Answers0