
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What are Getters/Setters methods for Python Class?
Encapsulation is one of the fundamental concepts in object-oriented programming (OOP). It describes the idea of wrapping data and the methods that work on data within one unit.
This restricts accessing variables and methods directly and can prevent the accidental modification of data. To prevent accidental change, an object's variable can only be changed by the object's method. Those types of variables are known as private variables.
In Python, getters and setters are methods used to access and modify private attributes of a class. These methods are a key part of encapsulation and help control how to access and update the data, especially when we want to protect the internal state of an object.
The 'getters' method in Python?
In Python, the getters() method is one of the OOP concepts. The getter methods are used to access the values of the attributes of the class
The variables that are declared as private cannot be accessed directly; using a getter method allows us to access them outside the class. Following is the syntax to define getter methods:
def get_attribute_name: return self._attribute_name
The 'setters' method in Python?
In Python, setters() methods are one of the OOP concepts. The setter methods are used to modify and update the value of an attribute of a class. Following is the syntax of setters() method -
def set_attribute_name(self, var): self._attribute_name= var
Implementation of getters() and setters() methods
In the following example, we have accessed the var1 using the getter method and updated it using the setter method -
class Sum: def __init__(self, var1, var2): self._var1 = var1 self._var2 = var2 print("Sum:", self._var1 + self._var2) # Getter for var1 def get_var1(self): return self._var1 # Setter for var1 def set_var1(self, value): self._var1 = value # Properly update the attribute # Create object obj1 = Sum(15, 78) # Get value using getter print("Getter method:", obj1.get_var1()) # Set new value using setter obj1.set_var1(100) # Verify the change print("Updated value after setter:", obj1.get_var1())
Following is an output of the above code -
Sum: 93 Getter method: 15 Updated value after setter: 100
Getter() and Setter() with property() function
In Python, property() is a built-in function that allows you to create properties with getter and setter behavior. Following is the syntax of the property function -
property(get_attribute_name, set_attribute_name, del_attribute_name(optional))
Here, we have binded the getter() and setter() using property() function -
class Sum: def __init__(self, var1, var2): self._var1 = var1 self._var2 = var2 print("Sum:", self._var1 + self._var2) # Getter method def get_var1(self): return self._var1 # Setter method def set_var1(self, value): self._var1 = value # Bind getter and setter to a property var1 = property(get_var1, set_var1) # Create object obj1 = Sum(85,13) # Get value using property print("Getter method:", obj1.var1) # Set new value using property obj1.var1 = 5 # Verify the change print("Updated value after setter:", obj1.var1)
Following is an output of the above code
Sum: 98 Getter method: 85 Updated value after setter: 5
Accessing Private variables using getter() method
The members of a class that are declared private are accessible within the class only; the private access modifier is the most secure access modifier. Data members of a class are declared private by adding a double underscore ?__' symbol before the data member of that class.
The private variables can be accessed using the getter() method. In the following example, we have accessed the private variable using getter() method -
class Sum: def __init__(self, var1, var2): self.__var1 = var1 #private variable 1 self.__var2 = var2 #private variable 2 print("Sum:", self.__var1 + self.__var2) # Getter method def get_var1(self): return self.__var1 # Create object obj1 = Sum(74,19) # Get value using property print("Accessing private variable using getter method:", obj1.get_var1())
Following is an output of the above code
Sum: 93 Accessing private variable using getter method: 74