Accessor and Mutator Methods in Python


Accessor and mutator methods in Python are used to access the private data of a class which cannot be accessed from outside the class. In object oriented programming the class object data is encapsulated i.e the object data is kept private and cannot be accessed from outside the object. The access to these private data from outside the object is provided using the Accessor and mutator method in python. These methods are also known as getter and setter methods in python. In this article we will understand Accessor and mutator methods with help of examples.

Accessor Methods

Accessor method is used to access object data. The private variables of the object can be accessed using the accessor methods. The accessor methods are declared public methods which return private member data of an object. The Accessor methods are also called getter methods as they are used to get an object data.

In Python the accessor method is defined using @property decorator. When the accessor method is called it returns the private member variable value of the object.

Example

In the below example we will define a class Person with a private variable _name. We then create a name accessor method which returns the value of the _name variable which is a private member variable of Person class. We can access the _name attribute value by creating a person object and accessing the value using the name accessor method.

class Person:
   def __init__(self, name):
      self.__name = name

   @property
   def name(self):
      return self.__name

person = Person("John")
print(person.name)  

Output

John

Mutator Method

Mutator methods are used to modify an object's private data. Mutator methods are also called setter methods as they are used to set/modify the value of an object private variable. Mutator methods are declared private which modifies the private value of the object variables.

In python mutator methods are defined using the @.setter decorator which specifies that the particular method behaves like a setter method. When the mutator method is called it sets the value of the object private variable.

Example

In the below example we define a Person class which has a private _name variable. We also define a name accessor method and a name mutator method using @property and @name.setter decorator respectively. The name mutator method modifies the value of the _name variable when a value parameter is passed to it while calling the function.

class Person:
   def __init__(self, name):
      self.__name = name

   @property
   def name(self):
      return self.__name

   @name.setter
   def name(self, value):
      self.__name = value

person = Person("John")
person.name = "Jane"
print(person.name)  

Output

Jane

Conclusion

Accessor and mutator methods are used in object oriented programming to provide access to private variables of the object. These methods are also called getter and setter methods as they are used to get and set/modify the private variables of the object respectively. The accessor and mutator method in python are defined using @property and @<variable_name>.setter decorator respectively.

Updated on: 13-Apr-2023

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements