Python Readonly Property,caching the property

PHOTO EMBED

Thu Feb 29 2024 06:52:15 GMT+0000 (Coordinated Universal Time)

Saved by @viperthapa #python

import math


class Circle:
    def __init__(self, radius):
        self._radius = radius
        self._area = None

    @property
    def radius(self):
        return self._radius

    @radius.setter
    def radius(self, value):
        if value < 0:
            raise ValueError('Radius must be positive')

        if value != self._radius:
            self._radius = value
            self._area = None

    @property
    def area(self):
        if self._area is None:
            self._area = math.pi * self.radius ** 2

        return self._area
Code language: Python (python)
content_copyCOPY

https://www.pythontutorial.net/python-oop/python-readonly-property/