from abc import ABC, abstractmethod class Bird(ABC): @abstractmethod def move(self): pass
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 Bird: def fly(self, altitude: int) -> None: return f"Flying at altitude" class Penguin(Bird): def fly(self, altitude: int) -> None: # Violation: Changes pre-condition (cannot fly) raise NotImplementedError("Penguins can't fly") Python 3- Deep Dive -Part 4 - OOP-
class Sparrow(FlyingBird): def move(self): return self.fly(100) def fly(self, altitude: int): return f"Flying at altitude"
class Employee: def __init__(self, name, salary): self.name = name self.salary = salary def calculate_pay(self): return self.salary * 0.8 # Business rule class Bird: def fly(self, altitude: int) -> None:
def generate_pdf_report(self): print(f"PDF: self.name") # Presentation
class NotificationService: # High-level def (self, sender: MessageSender): # Injected dependency self._sender = sender class Bird: def fly(self
from abc import ABC, abstractmethod class DiscountStrategy(ABC): @abstractmethod def apply(self, amount: float) -> float: pass
def save_to_db(self): print(f"Saving self.name to DB") # Persistence