Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Accessing Attributes and Methods in Python
Next article icon

Accessor and Mutator methods in Python

Last Updated : 01 Jul, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

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
 

Accessor and Mutator methods

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 operations
import array
    
# initializing array with array values
# initializes array with signed integers
arr = array.array('i', [5, 1, 3, 4, 2, 2, 7])
 
# Accesses the index of the value in argument
print (arr.index(2))
 
# Accesses the number of time the
# stated value is present
print (arr.count(2))
 
# Mutates the array list
(arr.append(19))
 
# Prints the array after alteration
print (arr)
 
 
Output: 
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 Car
class 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 object
myCar = Car('Ford');
 
# Accesses the value of the variable
# using Accessor method and then
# prints it
print (myCar.get_make())
 
# Modifying the value of the variable
# using Mutator method
myCar.set_make('Porche')
 
# Prints the modified value
print (myCar.get_make())
 
 
Output: 
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.
 



Next Article
Accessing Attributes and Methods in Python
author
nidhi1352singh
Improve
Article Tags :
  • Python
  • python-oop-concepts
Practice Tags :
  • python

Similar Reads

  • Accessing Attributes and Methods in Python
    In Python, attributes and methods define an object's behavior and encapsulate data within a class. Attributes represent the properties or characteristics of an object, while methods define the actions or behaviors that an object can perform. Understanding how to access and manipulate both attributes
    4 min read
  • Extend Class Method in Python
    In Python, class methods are functions that are bound to the class rather than the instance of the class. This means they receive the class (cls) as the first argument instead of the instance (self). Extending a class method refers to enhancing or customizing the behavior of an inherited class metho
    3 min read
  • Dunder or magic methods in Python
    Python Magic methods are the methods starting and ending with double underscores '__'. They are defined by built-in classes in Python and commonly used for operator overloading.  They are also called Dunder methods, Dunder here means "Double Under (Underscores)". Python Magic MethodsBuilt in classes
    7 min read
  • Adapter Method - Python Design Patterns
    Adapter method is a Structural Design Pattern which helps us in making the incompatible objects adaptable to each other. The Adapter method is one of the easiest methods to understand because we have a lot of real-life examples that show the analogy with it. The main purpose of this method is to cre
    4 min read
  • Is __init__() a private method in Python?
    Here in this article we are going to find out whether __init__() in Python is actually private or not. So we might come across many questions like What actually is __init__() method?What actually are private methods?And, if __init__() is private then how can we access it outside of a class? We have
    3 min read
  • Define and Call Methods in a Python Class
    In object-oriented programming, a class is a blueprint for creating objects, and methods are functions associated with those objects. Methods in a class allow you to define behavior and functionality for the objects created from that class. Python, being an object-oriented programming language, prov
    3 min read
  • Python List append() Method
    append() method in Python is used to add a single item to the end of list. This method modifies the original list and does not return a new list. Let's look at an example to better understand this. [GFGTABS] Python a = [2, 5, 6, 7] # Use append() to add the element 8 to the end of the list a.append(
    3 min read
  • Access Modifiers in Python : Public, Private and Protected
    Prerequisites: Underscore (_) in Python, Private Variables in Python Encapsulation is one of the four principles used in Object Oriented Paradigm. It is used to bind and hide data to the class. Data hiding is also referred as Scoping and the accessibility of a method or a field of a class can be cha
    9 min read
  • Memento Method Design Pattern in Python
    The Memento Design Pattern is a behavioral design pattern that allows you to capture and restore an object's state without revealing its internal structure. This pattern is particularly useful in implementing undo mechanisms. In this article, we will explore the Memento Method Design Pattern in Pyth
    8 min read
  • Are Lists Mutable in Python?
    Yes, lists are mutable in Python. This means that once a list is created, we can modify it by adding, removing or changing elements without creating a new list. Let's explore some examples that demonstrate how lists can be modified in Python: Changing Elements in a ListSince lists are mutable, you c
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences