Getter and Setter in Python

PythonServer Side ProgrammingProgramming

For the purpose of data encapsulation, most object oriented languages use getters and setters method. This is because we want to hide the attributes of a object class from other classes so that no accidental modification of the data happens by methods in other classes.

As the name suggests, getters are the methods which help access the private attributes or get the value of the private attributes and setters are the methods which help change or set the value of private attributes.

Accessing Private Attribute

Below we write code to create a class, initialize it and access it variables without creating any additional methods.

Example

class year_graduated:
   def __init__(self, year=0):
      self._year = year

# Instantiating the class
grad_obj = year_graduated()
#Printing the object
print(grad_obj)
#Printing the object attribute
print(grad_obj.year)

Output

Running the above code gives us the following result -

<__main__.year_graduated object at 0x00F2DD50>
0

While the first print statement gives us the details of the object created, the second print object gives us the default value of the private attribute.

Using getters and setters

In the below examples we will make a class, initialize is and then add a getter and setter method to each of them. Then access the variables in these methods by instantiating the class and using these getter and setter methods. So you can hide your logic inside the setter method.

Example

 Live Demo

class year_graduated:
   def __init__(self, year=0):
      self._year = year

   # getter method
   def get_year(self):
      return self._year

# setter method
def set_year(self, a):
self._year = a

grad_obj = year_graduated()
# Before using setter
print(grad_obj.get_year())

# After using setter
grad_obj.set_year(2019)
print(grad_obj._year)

Output

Running the above code gives us the following result:

0
2019

Making the Attributes Private

In the next example we see how to make the methods private so that the variables in it cannot be manipulated by external calling functions. They can only be manipulated by functions inside the class. They become private by prefixing them with two underscores.

Example

 Live Demo

class year_graduated:
   def __init__(self, year=32):
      self._year = year

   # make the getter method
   def get_year(self):
      return self.__year

   # make the setter method
   def set_year(self, a):
      self.__year = a

grad_obj = year_graduated()
print(grad_obj._year)

# Before using setter
print(grad_obj.get_year())
#
# # After using setter
grad_obj.set_year(2019)
print(grad_obj._year)

Output

Running the above code gives us the following result:

32
AttributeError: 'year_graduated' object has no attribute '_year_graduated__year'

Reading Values from Private Methods

No we can access the private attribute values by using the property method and without using the getter method.

Example

 Live Demo

class year_graduated:
   def __init__(self, year=32):
      self._year = year

   @property
   def Aboutyear(self):
      return self.__year

   @Aboutyear.setter
   def Aboutyear(self, a):
      self.__year = a

grad_obj = year_graduated()
print(grad_obj._year)

grad_obj.year = 2018
print(grad_obj.year)

Output

Running the above code gives us the following result:

32
2018
raja
Published on 02-Jan-2020 14:38:42
Advertisements