Python – Getter and Setter Methods

Getter and Setter Methods in Python

In this tutorial, we will explain the concept of getter and setter methods in Python.

Getter methods are used to retrieve the value of an attribute, while setter methods are used to modify the value of an attribute in a controlled manner. This process of controlling the properties of a class instance is kind of Encapsulating the details of an object from outside the class.

Use of getter and setter methods

Getter and setter methods are used when you want to control how attribute values are accessed and modified. They provide an additional layer of abstraction and allow you to enforce certain rules or validations while working with attributes.

Examples

1. Using Getter and Setter Methods in Person class

In this example, we’ll define a class Person with getter and setter methods for its attribute.

Python Program

class Person:
    def __init__(self, name, age):
        self._name = name
        self._age = age
    
    # Getter method
    def get_name(self):
        return self._name
    
    # Setter method
    def set_age(self, age):
        if age >= 0:
            self._age = age
        else:
            print("Age cannot be negative")
    
    # Getter method
    def get_age(self):
        return self._age

# Create an instance of Person
person = Person("Alice", 30)

# Use getter method
print(f"Name: {person.get_name()}")
print(f"Age: {person.get_age()}")

# Use setter method
person.set_age(25)
print(f"Updated age: {person.get_age()}")
Run Code Copy

Output

Name: Alice
Age: 30
Updated age: 25

In this example, the get_name() and get_age() methods are getter methods that allow controlled access to the private attributes: _name and _age respectively.

The set_age() method is a setter method that enforces a condition before updating the attribute.

2. Getter and Setter methods using Property Decorators

Python provides property decorators as a more concise way to create getter and setter methods.

In this example, the @property decorator is used to define the getter method, and the @radius.setter decorator is used to define the setter method. These decorators make the getter and setter methods look like regular attribute accesses.

Python Program

class Circle:
    def __init__(self, radius):
        self._radius = radius
    
    @property
    def radius(self):
        return self._radius
    
    @radius.setter
    def radius(self, value):
        if value >= 0:
            self._radius = value
        else:
            print("Radius cannot be negative")

circle = Circle(5)
print(f"Initial radius: {circle.radius}")
circle.radius = 7
print(f"Updated radius: {circle.radius}")
Run Code Copy

Output

Initial radius: 5
Updated radius: 7

Consider the following statement.

print(f"Initial radius: {circle.radius}")

When circle.radius is executed, the getter method radius() is called.

Now, consider the following statement.

circle.radius = 7

When this statement is executed, the setter method radius(value) is called, with 7 passed as argument for the value parameter.

Summary

In this tutorial of Python Classes and Objects, we have learnt about Getter and setter methods in a class, how to use them to control how attribute values are accessed and modified in a class, with some example programs.

Related Tutorials

Code copied to clipboard successfully 👍