The @property Decorator for Getter/Setter Methods in Classes in Python


Use the @property decorator to define getter, setter, and deleter functions as properties of a class.

Source Code

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:
            raise ValueError("Radius must be non-negative")
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments