I have a question on (which could be said to be an extension on another question but I couldnt comment yet) python - attribute x is a property object which provides interface to this private variable
class Celsius:
def __init__(self, temperature=0):
self.temperature = temperature
def to_fahrenheit(self):
return (self.temperature * 1.8) + 32
def get_temperature(self):
print("Getting value")
return self._temperature
def set_temperature(self, value):
if value < -273:
raise ValueError("Temperature below -273 is not possible")
print("Setting value")
self._temperature = value
temperature = property(get_temperature, set_temperature)
So initially I was having problems with "maximum recurson"/ stack overflow error in which cause by the temperature keep calling the setter which then calls itself.
The solution seems to be by convention adding underscore infront => "_temperature" or any other name which makes it becomes a private variable so it stops calling itself
My question still remains after searching for all questions related is that how does the function knows to store the value of "temperature" to "_temperature" unless it is really obvious but I could not figure it out.
Thank you for helping out, new Python learner, started in Java so the private and public concept kinda conflicts with over here.