Advertisement
Advertisement

Python 3- Deep Dive -part 4 - Oop- 🏆

class DiscountCalculator: def calculate(self, amount: float, strategy: DiscountStrategy) -> float: return strategy.apply(amount) Subtypes must be substitutable for their base types. Deep Dive Issue: Python's duck typing hides LSP violations. A subclass might accept different argument types or raise unexpected exceptions.

class VIPDiscount(DiscountStrategy): def apply(self, amount: float) -> float: return amount * 0.8 Python 3- Deep Dive -Part 4 - OOP-

class FlyingBird(Bird): @abstractmethod def fly(self, altitude: int): pass class VIPDiscount(DiscountStrategy): def apply(self

def save_to_db(self): print(f"Saving self.name to DB") # Persistence amount: float) -&gt

class Employee: def __init__(self, name, salary): self.name = name self.salary = salary def calculate_pay(self): return self.salary * 0.8 # Business rule

from abc import ABC, abstractmethod class MessageSender(ABC): # Abstraction @abstractmethod def send(self, message: str) -> None: pass

from abc import ABC, abstractmethod class DiscountStrategy(ABC): @abstractmethod def apply(self, amount: float) -> float: pass