I am taking a Python OOP course and, by playing with the code in the course, I stumbled upon something interesting.
Suppose I have this code:
class genericClass:
def __init__(self):
self.__secret = "secret"
generic_object = genericClass()
print(generic_object.__secret)
When I run this code I get the following error:
AttributeError: 'genericClass' object has no attribute '__secret'
But if I do the following:
class genericClass:
def __init__(self):
self.__secret = "secret"
generic_object = genericClass()
generic_object.__secret = "not a secret anymore" #set a different value for the '__secret attribute
print(generic_object.__secret)
Everything works and the message is printed to my terminal.
How does this happen? Does this specific instance now have 2 '__secret' attributes? If yes, how does Python differentiate between the two? If not, what is going on?