Setters and getters are implentations of Object Oriented designs where encapsulated data are exposed from the object.

A setter executes an object’s method to update properties of that object.
A getter executes and returns a value from an object’s method.

Method One - property(getter, setter)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
class Animal:
  def __init__(self):
    self._age = 30
  
  def getAge(self):
    return self._age
  
  def setAge(self, age):
    self._age = age
  
  age = property(getAge, setAge)  

Method Two - Function decorator

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
class Animal:
  def __init__(self):
    self._age = 30
  
  @property
  def age(self):
    return self._age
  
  @age.setter
  def age(self, age):
    self._age = age