I have a class with some class attributes
class Test:
attr = {#lots of data}
class Sub(Test):
def __init__(self, param):
self.param = param
Now if I initialize lots of subclasses [Sub(i) for i in range(10000)] each instance of Sub will have an attribute attr. Does that copy the data in attr 10000 times or does each reference of attr point to the same dictionary?
Edit:
Could someone also explain this behavior if attr is a class attribute and the data does not get copied, what exactly happens here
test1 = Sub(1)
test2 = Sub(2)
test1.attr == test2.attr. # True
test1.attr = {}
test1.attr == test2.attr # False
I was expecting that assigning an empty dictionary to the attr in the instance would overwrite the reference to the class attribute for all instances but that is not the case
attris class attribute. No copy is done whenSub(i)is created.test1.attr['foo'] = 0, then all instances see the update.Sub(1).attr is Sub(1).attr # (is True)andSub(1).param is Sub(2).param # (is False).