Accessor and Mutator methods in Python - GeeksforGeeks

01 Jul, 2021
In Python, class is a prototype for objects which is a user-defined type. It specifies and defines objects of the same type, a class includes a cluster of data and method definitions. Moreover, an object is a single instance of a class but you can create many objects from a single class.Note: For more information, refer to Python Classes and Objects 
So, you all must be acquainted with the fact that the internal data of an object must be kept private. But there should be some methods in the class interface that can permit the user of an object to access and alter the data (that is stored internally) in a calm manner. Therefore, for that case we have two methods namely Accessors and Mutators that are helpful in accessing and altering respectively, internally stored data.  
Accessor Method: This method is used to access the state of the object i.e, the data hidden in the object can be accessed from this method. However, this method cannot change the state of the object, it can only access the data hidden. We can name these methods with the word get.  
Mutator Method: This method is used to mutate/modify the state of an object i.e, it alters the hidden value of the data variable. It can set the value of a variable instantly to a new value. This method is also called as update method. Moreover, we can name these methods with the word set.  
Below examples illustrate the use of Accessor and Mutator methods in Python: Example 1:  
Python
# Python program to illustrate the use of# Accessor and Mutator methods # importing "array" for array operationsimport array    # initializing array with array values# initializes array with signed integersarr = array.array('i', [5, 1, 3, 4, 2, 2, 7]) # Accesses the index of the value in argumentprint (arr.index(2)) # Accesses the number of time the# stated value is presentprint (arr.count(2)) # Mutates the array list(arr.append(19)) # Prints the array after alterationprint (arr)
4
2
array('i', [5, 1, 3, 4, 2, 2, 7, 19])
 
So, here the index() and count() method only accesses the data so they are accessor methods but the append() method here modifies the array so its a mutator method. Example 2:  
Python
# Python program to illustrate the use of# Accessor and Mutator methods # Defining class Carclass Car:     # Defining method init method with a parameter    def __init__(self, carname):        self.__make = carname     # Defining Mutator Method    def set_make(self, carname):        self.__make = carname     # Defining Accessor Method    def get_make(self):        return self.__make # Creating an objectmyCar = Car('Ford'); # Accesses the value of the variable# using Accessor method and then# prints itprint (myCar.get_make()) # Modifying the value of the variable# using Mutator methodmyCar.set_make('Porche') # Prints the modified valueprint (myCar.get_make())
Ford
Porche
 
So, here the name of the car was accessed using Accessor method i.e, get_make and then it was modified using Mutator method i.e, set_make. 
akshaysingh98088
python-oop-concepts
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
How to Install PIP on Windows ?
How to drop one or multiple columns in Pandas Dataframe
How To Convert Python Dictionary To JSON?
Check if element exists in list in Python
Python | Pandas dataframe.groupby()
Defaultdict in Python
Python | Get unique values from a list
Python Classes and Objects
Python | os.path.join() method
Create a directory in Python