Understanding Accessor and Mutator Methods in Python
June 9, 2024 | by a.a.mamun595@gmail.com
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
Person
class is defined with an__init__
method, which is the constructor method. This method initializes the instance variables_name
and_age
when an object of the class is created.
- The
- Accessor Methods:
get_name
andget_age
methods are defined to return the values of_name
and_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
Person
class,john
, is created with the name “John” and age 30. - The
get_name
method is called on thejohn
object, returning “John”. - The
get_age
method is called on thejohn
object, 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
Person
class is expanded to include mutator methodsset_name
andset_age
.
- The
- Accessor Methods:
get_name
andget_age
methods remain unchanged and continue to provide read-only access to the object’s data.
- Mutator Methods:
set_name
method allows updating the_name
attribute of thePerson
object.set_age
method allows updating the_age
attribute. 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
john
object is created with the initial values “John” and 30. - The
set_name
method is called to change the name to “Johnny”. Theget_name
method then confirms the change. - The
set_age
method is called to change the age to 35. Theget_age
method confirms this change. - An attempt to set a negative age results in an error message, demonstrating the validation logic in the
set_age
method.
- 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! 🐍🔍
Sources:
RELATED POSTS
View all