I'm learning about encapsulation and abstraction in python and i came across the property function and decorator. The common example is something like this.
class Celsius():
def __init__(self, temperature = 0):
self.set_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)
I dont understand why the property function is assigning the descriptor to temperature
and not self.temperature
. Isn't it suppouse to create a getter or setter functionality to a Instance, not to the class?
something like
self.temperature = property(get_temperature,set_temperature)
using
test = Celsius()
pprint(test.__dict__)
returns that the instance object just have the self._temperature
attribute (Which we are trying to make private).
using pprint(Celsius.__dict__)
returns that is actually the class that have the temperature attribute that we are accessing when using the objects, which to my understanding doesn't make sense since i am interested in creating functionality to the instance and access the instance atributes, not the class attributes.
Thanks in advance :)