I have following code snippet:
class House:
area = "Munich"
def __init__(self, street, number):
self.street = street
self.num = number
h1 = House("Offenbachstarsse", 123)
h2 = House("Offenbachstarsse", 145)
print(h1.street + ',' + str(h1.num) + ',' + h1.area)
print(h2.street + ',' + str(h2.num)+ ',' + h2.area)
output ** Offenbachstarsse,123,Munich Offenbachstarsse,145,Munich **
h2.area = "Regensburg"
print(h1.street + ',' + str(h1.num) + ',' + h1.area)
print(h2.street + ',' + str(h2.num)+ ',' + h2.area)
output ** Offenbachstarsse,123,Munich Offenbachstarsse,145,Regensburg **
House.area = "Germany"
print(h1.street + ',' + str(h1.num) + ',' + h1.area)
print(h2.street + ',' + str(h2.num)+ ',' + h2.area)
output ** Offenbachstarsse,123,Germany Offenbachstarsse,145,Regensburg **
could someone please explain, how do updates to class attributes work? when an update to class attribute is done using the class, why does it only change the h1 object area and not h2 object area ?
I understand that the class attribute "area" when changed using the object, changes only the value of the class attribute of the used object i.e. h2.area ="Regensburg" updates only h2.area, whereas my expectation was that changes done in the class attribute using the class should be reflected in all the object instances as well. example: House.area = "Germany" would lead to h2.area = "Germany" and h1.area = "Germany". But I see that the h2.area is still showing the local update. Why is that ?