Here is my code:
class Dog:
def __init__(self,name,age):
self.n = name
self.__a = age
lucy=Dog("lucy",2)
print(lucy.__a)
jack=Dog("jack",3)
jack.__a=10
print(jack.__a)
When I run it, print(lucy.__a)
gives me an error, which is understandable, because __a
is a private instance variable. What confuses me a bit is that, print(jack.__a)
works perfectly fine. I think it's because we have the jack.__a=10
before it. Could anyone explain to me exactly what is going on?