""" Now that we know the basics of classes, variables, and functions, we will move on to more abstract ideas. I'm also going to introduce type hinting; this reduces ambiguity and improves readability. Key Concepts: **Encapsulation** is the concept of grouping related data and methods within a class and restricting access to them to control how they are used. **Access Modifiers** are used to determine who/what can access a certain attribute or method. - **Public**: Accessible from any part of the code. Used for attributes and methods intended to be part of the class's interface for external use. - **Protected**: Intended for internal use within the class and its subclasses. "_value" represents it. Useful for attributes/methods that should be accessible in subclasses but not publicly. - **Private**: Only accessible within the class, represented by "__value". Useful for securing sensitive data or internal methods that should not be altered externally. IMPORTANT: Python does not strictly enforce access control, so these conventions mainly serve as guidelines. """ from abc import ABC, abstractmethod # This is the abstraction library in Python from typing import Optional # Type hinting, not needed but reduces ambiguity. Note: not enforced at runtime. class Shape(ABC): # An abstract class, provides a blueprint for other classes to inherit. @abstractmethod def area(self) -> float: """Implemented to ensure each subclass contains this method.""" # This method is required in all subclasses, ensuring they provide their own area calculation. class Rectangle(Shape): def __init__(self, width: float, height: float): self.__width = width # A private attribute to store width. self.__height = height # A private attribute to store height. @property # This allows access to the private 'width' attribute in a controlled way. def width(self) -> float: return self.__width @width.setter # This allows you to set the value of 'width' after initialization. def width(self, value: float): if value <= 0: raise ValueError("Width must be positive.") self.__width = value @property # This allows access to the private 'height' attribute in a controlled way. def height(self) -> float: return self.__height @height.setter # This allows you to set the value of 'height' after initialization. def height(self, value: float): if value <= 0: raise ValueError("Height must be positive.") self.__height = value def area(self) -> float: return self.__width * self.__height # Calculates the area of the rectangle. def set_dimensions(self, width: Optional[float] = None, height: Optional[float] = None): if width is not None: self.width = width # Sets 'width' using the setter. if height is not None: self.height = height # Sets 'height' using the setter. rect = Rectangle(5, 10) print(rect.area()) # Prints the area of the rectangle (5 * 10 = 50). rect.set_dimensions(width=8) # Updates 'width' using the setter, height remains the same. print(rect.area()) # Prints the new area of the rectangle (8 * 10 = 80).