I am learning to use @property decorators. I have the following two code snippets:
Code 1
class P2:
def __init__(self, x):
self.x = x
@property
def x(self):
return self.y
@x.setter
def x(self, x):
if x < 0:
self.y = x
elif x > 1000:
self.y = 1000
else:
self.y = x
p1 = P2(7600)
print(p1.y)
Code2
class P2:
def __init__(self, x):
self.x = x
@property
def x(self):
return self.__x
@x.setter
def x(self, x):
if x < 0:
self.__x = x
elif x > 1000:
self.__x = 1000
else:
self.__x = x
p1 = P2(7600)
print(p1.__x)
To obtain the code2 I replace y by __x in code1. But the issue is that the code1 is running perfectly fine but the code2 is giving an error 'P2' object has no attribute '__x'.
My understanding is that y and __x are merely two variables, they should behave in the same way, so in my opinion code2 and code1 are identical and both should give same output.
But, it is not happening. What is wrong in my understanding?