DEV Community

vinayak
vinayak

Posted on • Originally published at itsvinayak.hashnode.dev on

Getter and Setter in Python

The primary purpose of the Getter and Setter in object-oriented programs is to ensure data encapsulation or hiding. But in python Getter and Setter works a little differently as Private variables in python are not really hidden and can be accessed from outside.

As a result getter and setter are usually used for setting but validation and logic for class field and to avoid direct access.

This Getter and setter property can be achieved in many ways using python

  • Using the normal functions with predefined nomenclature

  • Using property() method Python Built-in Functions

  • Using @property decorators

Using the normal functions with predefined nomenclature

According to nomenclature any function or variable with a double underscor e is a private function for a class.

Example:

__name = 'vinayak' ## or def __my_name(): return "vinayak" 
Enter fullscreen mode Exit fullscreen mode

and any method with get and set append in front is treated as the getter and setter methods for that variable.

class Student(object): def __init__ (self, name: str) -> None: self.name: str = name self.__marks: int = 0 def set_marks(self, marks: int): """ setter method Set Method in python """ self.__marks = marks def get_marks(self): """ getter method Get Method in python """ return self.name + " got " + str(self.__marks) def __str__ (self) -> str: return "student name is " + self.name s = Student("vinayak") print(s) s.set_marks(10) marks_s_got = s.get_marks() print(marks_s_got) 
Enter fullscreen mode Exit fullscreen mode

Output:

student name is vinayak

vinayak got 10

Using property() method Python Built-in Functions

property() method in python takes four arguments fget , fset , fdel , and doc where fget is used to retrieve an attribute value, fset is used to set a value for an attribute, and fdel to delete attribute value, whereas the doc is just a docstring for attribute.

class Student(object): def __init__ (self, name: str) -> None: self.name: str = name self.__marks: int = 0 def set_marks(self, marks: int): """ setter method Set Method in python """ self.__marks = marks def get_marks(self): """ getter method Get Method in python """ return self.name + " got " + str(self.__marks) def del_marks(self): """ Del Method """ self.__marks = 0 return 1 def __str__ (self) -> str: return "student name is " + self.name docString = "getter and setter for marks variable" marks = property(get_marks, set_marks, del_marks, docString) s = Student("vinayak") print(s) s.marks = 10 marks_s_got = s.marks print(marks_s_got) del s.marks print(s.marks) 
Enter fullscreen mode Exit fullscreen mode

Output:

student name is vinayak

vinayak got 10

vinayak got 0

Using @property decorators

As we have seen the property() method, the same set of behaviour can also be achived by property decorators. @property decorators is one of the python's built-in decorators.

class Student(object): def __init__ (self, name: str) -> None: self.name: str = name self.__marks: int = 0 @property def marks(self): """ getter method Get Method in python """ return self.name + " got " + str(self.__marks) @marks.setter def marks(self, marks: int): """ setter method Set Method in python """ self.__marks = marks @marks.deleter def marks(self): """ Del Method """ self.__marks = 0 return 1 def __str__ (self) -> str: return "student name is " + self.name s = Student("vinayak") print(s) s.marks = 10 marks_s_got = s.marks print(marks_s_got) del s.marks print(s.marks) 
Enter fullscreen mode Exit fullscreen mode

Output:

student name is vinayak

vinayak got 10

vinayak got 0

Top comments (0)