Understanding Python Properties

class Celsius:
def __init__(self, temperature = 0):
self.temperature = temperature
def fahrenheit(self):
return (self.temperature * 1.8) + 32
def get_temperature(self):
print("Getting value")
return self._temperature
def set_temperature(self, value):
print("Setting value")
self._temperature = value
temperature = property(get_temperature,set_temperature)
>>>c.temperature
Getting value
0
class Celsius:
def __init__(self, temperature = 0):
self.temperature = temperature
def fahrenheit(self):
return (self.temperature * 1.8) + 32
@property
def temperature(self):
print("Getting value")
return self._temperature
@temperature.setter
def temperature(self, value):
print("Setting value")
self._temperature = value

The article was originally published at MicroPyramid blog.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
MicroPyramid

MicroPyramid

Python, Django, Android and IOS, reactjs, react-native, AWS, Salesforce consulting & development company