Advertisement
Beginner Python, Descriptors & Properties

What does the @property decorator do, and how does it let a method be accessed like an attribute?

Short answer

@property turns a method into a computed, READ-ONLY (by default) attribute — calling it WITHOUT parentheses runs the underlying method and returns its result, letting you expose computed values with clean attribute syntax instead of requiring an explicit method call.

Save Python mock interview
Advertisement
AModel answer
@property turns a method into a computed, READ-ONLY (by default) attribute — calling it WITHOUT parentheses runs the underlying method and returns its result, letting you expose computed values with clean attribute syntax instead of requiring an explicit method call.
class Circle:
    def __init__(self, radius):
        self.radius = radius
    @property
    def area(self):
        return 3.14159 * self.radius ** 2

c = Circle(5)
print(c.area)  # 78.53975 -- accessed like an attribute, no parentheses needed
Real-world example
Exposing a computed value (like area, or a full_name derived from first_name/last_name) as if it were a plain attribute.
Likely follow-ups
  • How would you also allow SETTING a value through a property, using the @x.setter decorator?
Related topics