Back to results for “OOP”
Python › Descriptors & Properties

How would you convert an existing plain attribute into a property later, without breaking any existing code that reads or writes it directly?

Beginner

Answer

Since attribute access syntax (obj.attr) looks identical whether it's a plain instance attribute or a property, you can freely replace a plain attribute with an @property (backed by a renamed private attribute) at any time without changing any external code that reads or assigns to it — this is a key reason Python developers don't need to write getters/setters defensively upfront.
# Before: plain attribute
class Temperature:
    def __init__(self, celsius):
        self.celsius = celsius

# After: converted to a property, external code (t.celsius) is unaffected
class Temperature:
    def __init__(self, celsius):
        self._celsius = celsius
    @property
    def celsius(self):
        return self._celsius
    @celsius.setter
    def celsius(self, value):
        self._celsius = value
Real-world example Adding validation to a class's attribute later in a project's life, without needing to update every place that already reads or writes that attribute.

Common follow-ups: Why is this seamless upgrade path considered a key advantage of Python's approach compared to languages that require explicit getters/setters from the start?