Getter and Setter in Python
Last Updated :
22 Mar, 2025
In Python, a getter and setter are methods used to access and update the attributes of a class. These methods provide a way to define controlled access to the attributes of an object, thereby ensuring the integrity of the data. By default, attributes in Python can be accessed directly. However, this can pose problems when attributes need validation or transformation before being assigned or retrieved.
- Getter: The getter method is used to retrieve the value of a private attribute. It allows controlled access to the attribute.
- Setter: The setter method is used to set or modify the value of a private attribute. It allows you to control how the value is updated, enabling validation or modification of the data before it’s actually assigned.
Python provides several ways to implement getter and setter methods:
Using normal function
In this approach, getter and setter methods are explicitly defined to get and set the value of a private variable. The setter allows setting the value and the getter is used to retrieve it.
Python
class Geek:
def __init__(self, age = 0):
self._age = age
# getter method
def get_age(self):
return self._age
# setter method
def set_age(self, x):
self._age = x
raj = Geek()
# setting the age using setter
raj.set_age(21)
# retrieving age using getter
print(raj.get_age())
print(raj._age)
Explanation:
- A Geek class is defined with an _age attribute.
- The get_age() method is the getter, which retrieves the value of _age.
- The set_age() method is the setter, which assigns a value to _age.
- The setter method is used to set the age and the getter method is used to retrieve it.
Using property() function
In this method, the property() function is used to wrap the getter, setter and deleter methods for an attribute, providing a more streamlined approach.
Python
class Geeks:
def __init__(self):
self._age = 0
# function to get value of _age
def get_age(self):
print("getter method called")
return self._age
# function to set value of _age
def set_age(self, a):
print("setter method called")
self._age = a
# function to delete _age attribute
def del_age(self):
del self._age
age = property(get_age, set_age, del_age)
mark = Geeks()
mark.age = 10
print(mark.age)
Outputsetter method called
getter method called
10
Explanation:
- A Geeks class is created with an internal _age attribute.
- The get_age() function retrieves the value, set_age() sets it and del_age() deletes the attribute.
- The property() function binds these methods to the age attribute.
- The age attribute is accessed and set using the property function.
Using @property decorators
In this approach, the @property decorator is used for the getter and the @<property_name>.setter decorator is used for the setter. This approach allows a more elegant way to define getter and setter methods.
Python
class Geeks:
def __init__(self):
self._age = 0
# using property decorator
# a getter function
@property
def age(self):
print("getter method called")
return self._age
# a setter function
@age.setter
def age(self, a):
if(a < 18):
raise ValueError("Sorry you age is below eligibility criteria")
print("setter method called")
self._age = a
mark = Geeks()
mark.age = 19
print(mark.age)
Outputsetter method called
getter method called
19
Explanation:
- The Geeks class uses the @property decorator for the getter method, which returns the age.
- The @age.setter decorator is used for the setter method, where a validation (age eligibility) is added.
- The setter is used to assign the value, while the getter retrieves it.
Similar Reads
__getitem__ and __setitem__ in Python Dunder methods are double underscored methods that are used to emulate the behavior of built-in types. They are predefined methods that simplify many operations that can be performed on a class instance, like __init__(), __str__(), __call__() etc. These methods are very helpful because they are used
3 min read
set() Constructor in Python In Python, the set() constructor is used to create a set object. A set is a built-in data type that stores an unordered collection of unique elements. The set() constructor can be used to create an empty set or convert other data types (like lists, tuples, or strings) into a set.Example:Python# Exam
2 min read
__getitem__() in Python __getitem__() is a special method (also known as a dunder or magic method) in Python that allows us to access an element from an object using square brackets, similar to how we access items in a list, tuple, or dictionary. It is commonly used to retrieve items from containers or objects that support
3 min read
Convert String to Set in Python There are multiple ways of converting a String to a Set in python, here are some of the methods.Using set()The easiest way of converting a string to a set is by using the set() function.Example 1 : Pythons = "Geeks" print(type(s)) print(s) # Convert String to Set set_s = set(s) print(type(set_s)) pr
1 min read
set() Function in python set() function in Python is used to create a set, which is an unordered collection of unique elements. Sets are mutable, meaning elements can be added or removed after creation. However, all elements inside a set must be immutable, such as numbers, strings or tuples. The set() function can take an i
3 min read