Properties

Properties are found in other languages as getters and setters. As those terms indicate, they are used to get data and set (modify) data. To gain an understanding of getters, setters, and Python properties, we'll start with an example problem:

Temperature class

Here, we create a class that defines the temperature in degrees Fahrenheit. The __init__() method takes temp as an argument; it also provides a default value for temp if none is provided. A conversion method to Celsius is also provided; it simply returns the conversion value but doesn't attempt to assign any values to any variables.

A new instance is created on line 82 and it is verified that it exists on line 83. Line 84 returns the temperature value; because no value was provided during the instance creation, the default value is used.

Line 85 assigns a new value to the temp variable, as shown on line 86. Line 87 shows that the Celsius conversion method works as expected.