1

If I have a child class that inherits from a base class whose init() adds an item to an array, as follows:

class TestBaseClass : 
    arr = []

    def __init__(self) : 
        print self.arr
        self.arr.append(1)
        print self.arr
        return

class TestChildClass1(TestBaseClass) : 
    foo = 1

Then I find it strange that when I instantiate the child class twice,

test1 = TestChildClass1()
test2 = TestChildClass1()

the second one seems to not have an empty array but contains the value 1 that was added by the first.

Why is that base class instance shared by the two and not created as a new instance? This does not seem to be intuitively how one would expect inheritance to behave.

0 Answers0