The use of get
and set
is demonstrated through several iterations of simple Python program that converts Celsius to Fahrenheit.
version1.py
is the main class definition.version1_test_001.py
is the client test harness.- The client's use of the server's variable
temperature
is exposed throughc.temperature
, which is a brittle style, since it exposes the server's data to the client. - A better way for the client to interact with the server is through
get
andset
functions.
- The client's use of the server's variable
verion2.py
is an updated version ofversion1.py
, but with two additions: get/set methods and error checking:- Get/Set Methods
- The local variable in
version1.py
calledtemperature
is changed inversion2.py
to_temperature
. - The leading underscore in the
_temperature
variable name is a convention to describe a private variable of a class. - In Python, private variables don't actually exist, so the leading underscrore is just a convention.
- Clients can circumvent the
c.get_temperature()
API simply throughc._temperature
.
- The local variable in
- Error Checking
- The set method will make sure the value being set is not below
-273.15
degrees Celsius, which is absolute zero Kelvin.
- The set method will make sure the value being set is not below
- Get/Set Methods
version2_test_001.py
is the client test harness.- Client code written to the previous server standard in
version1.py
with thec.temperature
convention must now be rewritten to use either the get or the set methods, viz.,c.get_temperature()
andc.set_temperature(new_value)
, which places significant rewrite burden on clients. But, clients shouldn't be accessing server private data in the first place. - The addition of the
get
prefix andset
prefix is verbose. - Verbs (e.g.,
get
andset
), per David Cheriton are unnecessary, undesired, and pollute and bloat the API, and thus should be avoided.
- Client code written to the previous server standard in
version3.py
eliminates clutter in the API by eliminatingget
andset
verbs through use of the@property
construction.- Note: the implementation here is better than what is published in the reference (see details below) because error checking is always used by virtue of server using its own
set
method in the constructor.
- Programiz.com Note: the
__init__
constructor accesses the local variables_temperature
directly, without use of the setter. But not using setter on construction, the error checking is circumvented, which is fragile coding. Instead, the constructor should have used the setter method. So in summary are thebad
andgood
methods:
# BAD
def __init__(self, temperature = 0):
self._temperature = temperature # avoids the setter, so avoids error checking, which is bad!
# GOOD
def __init__(self, temperature = 0):
self.temperature = temperature # uses the setter, which includes error checking, which is good!
- Python 3 Docs
- This makes it possible to create read-only properties easily using
property()
as a decorator.
- This makes it possible to create read-only properties easily using