Skip to content
Grace Li
Go back

Pythonic Encapsulation: The Power of @property

Edit page

Introduction

If you come from a Java or C++ background, your instinct when creating a class is likely to make all attributes private and write getVariable() and setVariable() methods for everything.

In Python, this is generally unnecessary. Python’s philosophy is to keep code simple and readable by using plain variables by default. You should only add protection logic if and when it becomes necessary, not for every single attribute upfront.

This might raise a concern: “If I start with simple variables, won’t I have to rewrite my code later if I need validation?”

The answer is no. This is exactly where the @property decorator comes in. It allows you to seamlessly upgrade a simple attribute to a method with logic, without breaking any code that uses your class.

The “Java” Way (Avoid This in Python)

In traditional languages, you write boilerplate code to future-proof your class.

# Don't do this in Python!
class StudentJavaStyle:
    def __init__(self, age):
        self._age = age

    def get_age(self):
        return self._age

    def set_age(self, age):
        if age < 0:
            raise ValueError("Age cannot be negative")
        self._age = age

# Usage is verbose
s = StudentJavaStyle(20)
s.set_age(21)
print(s.get_age())

The Pythonic Way: Start Simple

In Python, you should start with public attributes. It is clean, readable, and perfectly acceptable.

class Student:
    def __init__(self, age, address):
        self.age = age
        self.address = address

s = Student(20, "123 Main St")
s.age = 21  # Direct access

But what happens if you later realize functionality is needed? What if users are setting negative ages? In Java, if you started with public fields, changing to a setter would break code for everyone using your library. In Python, it does not.

Using @property for Validation

You can seamlessly transition a public attribute to a managed property without changing the interface handling the class.

Let’s enforce that age must be positive and address cannot be empty.

class Student:
    def __init__(self, age, address):
        self._age = age
        self._address = address

    # --- Age Property ---
    @property
    def age(self):
        """The getter for age"""
        return self._age

    @age.setter
    def age(self, value):
        """The setter for age with validation"""
        if not isinstance(value, int) or value < 0:
            raise ValueError("Age must be a positive integer")
        self._age = value

    # --- Address Property ---
    @property
    def address(self):
        return self._address

    @address.setter
    def address(self, value):
        if not value or len(value.strip()) == 0:
            raise ValueError("Address cannot be empty")
        self._address = value

# Usage remains exactly the same as the simple version!
s = Student(20, "123 Main St")

s.age = 25       # Calls the setter automatically
print(s.age)     # Calls the getter automatically

try:
    s.age = -5   # Raises ValueError
except ValueError as e:
    print(e)

Do I Need Both Getter and Setter?

No.

ConfigurationResultNote
Getter Only (@property)Read-Only AttributeGreat for constants or computed values.
Getter + SetterRead-Write AttributeThe standard way to add validation.
Setter OnlyImpossibleSyntax limitation. You must define the getter first.
class Circle:
    def __init__(self, radius):
        self.radius = radius

    @property
    def area(self):
        """I am read-only! You cannot set me."""
        return 3.14 * (self.radius ** 2)

c = Circle(5)
print(c.area) # Works: 78.5
# c.area = 100 # Error: AttributeError: can't set attribute

When to Use @property

You should not use @property for every single attribute. If you don’t have validation logic, just use a plain public attribute (self.name).

Only convert to a property if:

  1. You need Validation: e.g., checking for negative numbers.
  2. Computed Attributes: e.g., calculating full_name from first and last.
  3. Refactoring: You need to add logic to legacy code without breaking the API.

Keep it simple by default, and add complexity (validation) only where you actually need it.


Edit page
Share this post on:

Previous Post
How to use Claude Code without breaking the bank (or hitting rate limits)
Next Post
Introduction to WFDB: Working with PhysioNet Data