What are Getters/Setters methods for Python Class?


In python we have different methods available to make our work very easy and simple. Among the methods available in python we have two methods namely getter and setter. These methods play a vital role in the object oriented programming language of python to hide the private variables.

These methods of python are not as same as the getters/setters methods in other object oriented programming languages. These are used in object oriented programming language for data encapsulation. These are used with the below conditions

  • The getters/setters in python are used to validate the logic for getting and setting a value.

  • In python to avoid the direct access of the hidden variables by the external user, other words we can say the getters and setters are used to avoid the modification or accessing the hidden variables by the external users.

  • Data Encapsulation is binding the data related to a particular method as one. By using this format, the data modification and accessing will be restricted.

  • We can achieve this data encapsulation in python by using the getters and setters methods.

  • The way of using the getters and setters in python is not that much easy. For using the getter method to get a value, we have to use get() and for using the setter method for setting a value, we have to use set().

  • The getter method gives the access to private attributes whereas the setter method used to set the property’s value.

Example

Let’s see an example to understand the getter and setter method in python. The following code can be used to work with the getter and setter methods.

  • In the code firstly we created a class with the name Tutorialspoint. Next within the class we initiated the class using the __init__ function.

  • After that to define the getter method, we created the function name get_name.

  • Next to define the setter method, we created the set_name function. Now calling the class Tutorialspoint and assigned it to the variable b.

  • Next we used the setter method to set the value python to the class b we used the set_name function by using python along with it.

  • Next for using the getter method we printed the get_name() function of class b.Next also printed the _name variable.

class Tutorialspoint:   
   def __init__(self, name = 0):   
      self._name = name   
      # using the getter method   
   def get_name(self):   
      return self._name   
      # using the setter method   
   def set_name(self, a):   
      self._name = a   
    
b = Tutorialspoint()   
    
#using the setter function  
b.set_name("Python")   
    
# using the getter function  
print(b.get_name())   
    
print(b._name)  

Output

The following is the output of the getter and setter methods usage in python object oriented programming language.

Python
Python

Example

Let’s see another example to understand the getter and setter method in python in a better way. The following code can be used to work with the getter and setter methods.

class python:   
   def __init__(self, x = 0):   
      self._x = x   
      # using the getter method   
   def get_x(self):   
      return self._x   
      # using the setter method   
   def set_x(self, a):   
      self._x = a   
    
b = python()   
    
#using the setter function  
b.set_x([100,200])   
    
# using the getter function  
print(b.get_x())   
    
print(b._x)  

Output

[100, 200]
[100, 200]

Updated on: 15-May-2023

410 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements