Getter and Setter Method
In Python, getter and setter methods are used to manage the values of instance variables in an object-oriented manner. These methods allow controlled access to an object’s attributes, often for the purpose of enforcing rules or validation before modifying or retrieving the value.
Although Python provides the ability to access object attributes directly, it is a good practice to use getter and setter methods to encapsulate the attribute access. This is especially useful when you need to add validation, logging, or other behaviors when attributes are accessed or changed.
In Python, getters and setters are typically implemented in the following ways:
1. Getter Method
A getter method is used to retrieve or access the value of a private instance variable.
2. Setter Method
A setter method is used to set or modify the value of a private instance variable. This allows validation or other operations before the value is changed.
Example of Getter and Setter in Python:
class Person:
def __init__(self, name, age):
self._name = name # Using underscore to indicate "protected" variable
self._age = age # Private-like attribute
# Getter method for name
def get_name(self):
return self._name
# Setter method for name
def set_name(self, name):
if len(name) > 0:
self._name = name
else:
print("Name cannot be empty!")
# Getter method for age
def get_age(self):
return self._age
# Setter method for age
def set_age(self, age):
if age > 0:
self._age = age
else:
print("Age must be a positive number!")
# Using getters and setters
person = Person("Alice", 30)
print(person.get_name()) # Output: Alice
print(person.get_age()) # Output: 30
person.set_name("Bob") # Setting name using setter
person.set_age(35) # Setting age using setter
print(person.get_name()) # Output: Bob
print(person.get_age()) # Output: 35
person.set_name("") # Output: Name cannot be empty!
person.set_age(-5) # Output: Age must be a positive number!
Explanation:
- Private attributes: The
self._name
andself._age
are used as private-like variables (underscore_
is a convention for private attributes). - Getter methods:
get_name()
andget_age()
are used to retrieve the values of_name
and_age
. - Setter methods:
set_name()
andset_age()
are used to set the values of_name
and_age
with validation checks.
3. Property Decorators (@property
)
Python also provides a more Pythonic way to implement getter and setter methods using property decorators. This allows you to access methods like attributes, without needing explicit method calls.
@property
: Used to define a getter method.@<property_name>.setter
: Used to define a setter method.
Using Property Decorators:
class Person:
def __init__(self, name, age):
self._name = name
self._age = age
# Getter using @property
@property
def name(self):
return self._name
# Setter using @<property_name>.setter
@name.setter
def name(self, name):
if len(name) > 0:
self._name = name
else:
print("Name cannot be empty!")
# Getter for age
@property
def age(self):
return self._age
# Setter for age
@age.setter
def age(self, age):
if age > 0:
self._age = age
else:
print("Age must be a positive number!")
# Using property getter and setter
person = Person("Alice", 30)
print(person.name) # Output: Alice
print(person.age) # Output: 30
person.name = "Bob" # Setting name via setter
person.age = 35 # Setting age via setter
print(person.name) # Output: Bob
print(person.age) # Output: 35
person.name = "" # Output: Name cannot be empty!
person.age = -5 # Output: Age must be a positive number!
Explanation:
- The
@property
decorator is used to define getter methods (name
andage
). - The
@name.setter
and@age.setter
decorators are used to define setter methods forname
andage
. - You access and modify attributes just like normal instance variables (
person.name
andperson.age
), but the underlying getter and setter methods are still being used.