I am new in python and started learning OOP in python. I have come to know that if I want to make an attribute private in python class, I just need to add a double underscore (__) in-front of the attribute name. It should not be accessible from outside, but if I try to re-assign it, it doesn't complain. It seems like another attribute is creating with same name and it is public, not private!!! It's a bit confusing!!
class Class1:
def __init__(self, x):
self.__x = x
def getX(self):
return self.__x
ob = Class1(10)
print(ob.getX())
ob.__x = 'This should not work!'
print(ob.__x)
print(ob.getX())
output:
10
This should not work!
10