class Circle: def __init__(self, radius): self._radius = radius @property def radius(self): return self._radius # Example usage: c = Circle(5) print(c.radius) # Output: 5
5
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: raise ValueError("Radius cannot be negative") self._radius = value # Example usage: c = Circle(5) print(c.radius) # Output: 5 c.radius = 10 print(c.radius) # Output: 10 # c.radius = -1 # Uncommenting this line will raise a ValueError
5 10
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: raise ValueError("Radius cannot be negative") self._radius = value @radius.deleter def radius(self): del self._radius # Example usage: c = Circle(5) print(c.radius) # Output: 5 del c.radius # print(c.radius) # Uncommenting this line will raise an AttributeError
5
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: raise ValueError("Radius cannot be negative") self._radius = value @property def area(self): import math return math.pi * self._radius ** 2 # Example usage: c = Circle(5) print(c.radius) # Output: 5 print(c.area) # Output: 78.53981633974483 (π * 5^2)
5 78.53981633974483
class Person: def __init__(self, first_name, last_name): self._first_name = first_name self._last_name = last_name @property def full_name(self): return f"{self._first_name} {self._last_name}" # Example usage: p = Person("John", "Doe") print(p.full_name) # Output: John Doe # p.full_name = "Jane Doe" # Uncommenting this line will raise an AttributeError
John Doe
from dataclasses import dataclass @dataclass class Rectangle: length: float width: float @property def area(self): return self.length * self.width # Example usage: r = Rectangle(5, 3) print(r.area) # Output: 15
15