class C2:
x = 2
z = 2
class C3:
w = 3
z = 3
def __init__(self):
self.w += 1
class C1(C2, C3):
x = 1
y = 1
I1 = C1()
I1.name = 'I1'
I2 = C1()
I2.name = 'I2'
print I1.w # 4
print I2.w # 4
print C3.w # 3
Can you explain me the results from last 3 prints? I can't find the logic in this :)
C3is a class, butI1andI2are instances.. I am not sure what the confusion is about here?self.<attribute name>always will create an instance attribute if there was none before. Reading an instance attribute where there is none will always fall through to the class to see if it is defined there. Soself.w + 1returns the sum of the class attributew, thenself.w =assigns that to the instance attributew.