I'm trying to understand how getters and setters work in python with properties, but I'm confused in the naming convention.
Is there a specific naming standard for the getter and setter? Why do I need to set the returning name of parameter with an "_" (underscore)? If I were to change it to just "self.color" instead of "self._color", the getter and setter no longer work. Also, if I were to change the name of the functions the print function is no longer is executed. Any clarification would be greatly appreciated, thanks!
class FavoriteColor:
def __init__(self, color):
self.color = color
@property
def color(self):
print('getter method')
return self._color
@color.setter
def color(self, x):
print('setter method')
self._color = x
obj1 = FavoriteColor('blue')