Could someone please explain to me why the following code treats the class variables n and lines differently, and how to get lines to behave the same way as n? Thanks in advance!
class A():
n = 0
lines = []
@classmethod
def Add(cls, line):
cls.n += 1
cls.lines.append(line)
class B(A):
pass
class C(A):
pass
B.Add('hello')
C.Add('world')
print(B.lines, B.n)
print(C.lines, C.n)
Output is:
['hello', 'world'] 1
['hello', 'world'] 1
i.e. The variable n is separate for the child classes whereas lines is not. Running on Python 3.7.4 for Windows 10.
Edit: I'd like to keep the implementation of lines inside the parent class if I can. I know I could re-declare lines in the child classes to get the behavior I want.