Open In App

Getter and Setter in Python

Last Updated : 22 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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) 

Output
21
21

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) 

Output
setter 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) 

Output
setter 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.


Next Article

Similar Reads