본문 바로가기
개념/혼자 공부하는 파이썬

python) Circle Class with Encapsulation and Property Decorators.

by kiseno 2024. 11. 14.
728x90
반응형
SMALL

import math

class Circle:
    def __init__(self, radius):
        self.__radius = radius

    def get_circumference(self):
        return 2 * math.pi * self.__radius

    def get_area(self):
        return self.__radius **2 * math.pi

    def get_radius(self):
        return self.__radius

    def set_radius(self, value):
        self.__radius = value
    @property
    def radius(self):
        return self.__radius
    @radius.setter
    def radius(self, value):
        if value <=0:
            raise TypeError("Radius must be greater than 0")
        self.__radius = value

print("decorator use to Getter and Setter")
circle = Circle(10)
print("first circle_radius =", circle.radius)
circle_radius = 2
print("change to circle_radius =", circle_radius)
print()

 

1-2. **Import and Class Definition**:
   - `import math`: Imports the `math` module to access mathematical functions.
   - `class Circle:`: Defines a class named `Circle`.

3-6. **Constructor**:
   - `def __init__(self, radius):`: The constructor method to initialize a new Circle instance with a specific radius.
   - `self.__radius = radius`: Initializes a private attribute `__radius` with the value passed to the constructor. The double underscore prefix makes `__radius` a private attribute, meaning it should not be accessed directly from outside the class.

7-9. **Circumference Method**:
   - `def get_circumference(self):`: Defines a method to calculate the circumference of the circle.
   - `return 2 * math.pi * self.__radius`: Returns the circumference using the formula \(2\pi r\).

10-12. **Area Method**:
   - `def get_area(self):`: Defines a method to calculate the area of the circle.
   - `return self.__radius **2 * math.pi`: Returns the area using the formula \(\pi r^2\).

13-15. **Radius Getter Method**:
   - `def get_radius(self):`: A method to get the current radius of the circle.
   - `return self.__radius`: Returns the private attribute `__radius`.

16-18. **Radius Setter Method**:
   - `def set_radius(self, value):`: A method to set a new radius for the circle.
   - `self.__radius = value`: Updates the private attribute `__radius` with a new value.

19-21. **Radius Property**:
   - `@property`: A decorator that defines `radius` as a property, allowing the radius to be accessed like an attribute but actually invoking the `get_radius` method.
   - `def radius(self):`: The method called when the `radius` property is accessed.

22-26. **Radius Property Setter**:
   - `@radius.setter`: A decorator that specifies the setter method for the `radius` property.
   - `def radius(self, value):`: The method called when a value is assigned to the `radius` property.
   - `if value <= 0:`: Checks if the new radius is greater than 0.
   - `raise TypeError("Radius must be greater than 0")`: Raises an error if the new radius is not greater than 0.
   - `self.__radius = value`: Sets the radius to the new value if it's valid.

28-34. **Demonstration of Use**:
   - Prints a statement about using decorators for getter and setter.
   - Creates an instance of `Circle` with a radius of 10.
   - Demonstrates accessing and changing the circle's radius using the `radius` property.

This code illustrates how to encapsulate data (the radius of the circle), restrict access to it, and provide a controlled way of modifying it (via getter/setter methods and properties). The property decorators make the interface to get and set the radius cleaner and more Pythonic.

728x90
반응형
LIST