When working with objects in Python, you’ll often encounter situations where you need to access or modify the internal data of an object. To achieve this, Python provides two types of methods: accessors and mutators. Let’s explore what these methods are and how they differ, along with detailed explanations of the code examples provided.
Accessor Methods (Read-Only)
What Are Accessor Methods?
Accessor methods allow you to retrieve information about an object’s state without altering that state. They provide read-only access to the internal data of an object. Typically, accessor methods are named with the prefix “get” (e.g., get_value()).
Use Cases for Accessor Methods:
- Retrieving the value of an attribute (e.g., getting the length of a list).
- Calculating derived values (e.g., computing the area of a shape based on its dimensions).
Example: Accessor Methods
Let’s create a Person class with accessor methods to get the person’s name and age:
class Person:
    def __init__(self, name, age):
        self._name = name
        self._age = age
    def get_name(self):
        return self._name
    def get_age(self):
        return self._age
# Usage
john = Person('John', 30)
print(john.get_name())  # Output: John
print(john.get_age())   # Output: 30
Detailed Explanation:
- Class Definition:
- The Personclass is defined with an__init__method, which is the constructor method. This method initializes the instance variables_nameand_agewhen an object of the class is created.
 
- The 
- Accessor Methods:
- get_nameand- get_agemethods are defined to return the values of- _nameand- _age, respectively. These methods do not modify the state of the object; they only return information about it.
 
- Object Creation and Usage:
- An instance of the Personclass,john, is created with the name “John” and age 30.
- The get_namemethod is called on thejohnobject, returning “John”.
- The get_agemethod is called on thejohnobject, returning 30.
 
- An instance of the 
Mutator Methods (Update Methods)
What Are Mutator Methods?
Mutator methods modify an object’s state by changing the internal data. They allow you to update the values of attributes. Typically, mutator methods are named with the prefix “set” (e.g., set_value()).
Use Cases for Mutator Methods:
- Changing the value of an attribute (e.g., updating the speed of a car).
- Adding or removing elements from a collection (e.g., appending an item to a list).
Example: Mutator Methods
Continuing with our Person class, let’s add mutator methods to set the person’s name and age:
class Person:
    def __init__(self, name, age):
        self._name = name
        self._age = age
    def get_name(self):
        return self._name
    def get_age(self):
        return self._age
    def set_name(self, new_name):
        self._name = new_name
    def set_age(self, new_age):
        if new_age >= 0:  # Ensuring the age is non-negative
            self._age = new_age
        else:
            print("Age cannot be negative")
# Usage
john = Person('John', 30)
print(john.get_name())  # Output: John
john.set_name('Johnny')
print(john.get_name())  # Output: Johnny
john.set_age(35)
print(john.get_age())   # Output: 35
john.set_age(-5)        # Output: Age cannot be negative
Detailed Explanation:
- Class Definition:
- The Personclass is expanded to include mutator methodsset_nameandset_age.
 
- The 
- Accessor Methods:
- get_nameand- get_agemethods remain unchanged and continue to provide read-only access to the object’s data.
 
- Mutator Methods:
- set_namemethod allows updating the- _nameattribute of the- Personobject.
- set_agemethod allows updating the- _ageattribute. It includes a check to ensure that the new age is non-negative, maintaining a valid state for the object.
 
- Object Creation and Usage:
- The johnobject is created with the initial values “John” and 30.
- The set_namemethod is called to change the name to “Johnny”. Theget_namemethod then confirms the change.
- The set_agemethod is called to change the age to 35. Theget_agemethod confirms this change.
- An attempt to set a negative age results in an error message, demonstrating the validation logic in the set_agemethod.
 
- The 
Summary
- Accessor methods provide read-only access to an object’s data.
- Mutator methods allow you to modify an object’s state.
- Choose the appropriate method based on whether you need to retrieve information or update it.
Accessor and mutator methods help maintain encapsulation by controlling how internal data is accessed and modified. Use them wisely in your Python classes! 🐍🔍


