consider the following code:
class animal:
def __init__(self, name):
self.__name = name
class dog(animal):
def __init__(self, owner, name):
self.__owner = owner
super(dog, self).__init__(name)
terrier = dog('A', 'B')
terrier.owner = 'C'
terrier.name = 'D'
print(terrier._dog__owner, terrier._animal__name)
print(terrier.owner, terrier.name)
The output is:
A B
C D
I understand that Python private variables are only protected by convention. But all other threads mention ._className__attributeName eg. terrier._dog__owner as the only way of altering variable values. Here I am able to alter them even using terrier.owner or terrier.name.
Strangely, both give a different output as shown above. So do terrier.owner or terrier.name create different instances from terrier._dog__name or terrier._animal__name? What exactly has happened here?
terrier.owneris a different name fromterrier._dog__owner. Why would you think otherwise? It's no different from doingterrier.blah = "foo". You can create whatever attributes you want.