Python 3- Deep Dive -part 4 - Oop- -
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
class Penguin(Bird): def move(self): return "Swimming" # No fly method. Substitutable for Bird. Clients should not be forced to depend on methods they do not use. Deep Dive Issue: Python has no explicit interface keyword. We use Protocol (PEP 544) or multiple ABCs . Fat protocols lead to NotImplementedError stubs. Python 3- Deep Dive -Part 4 - OOP-
from abc import ABC, abstractmethod class DiscountStrategy(ABC): @abstractmethod def apply(self, amount: float) -> float: pass
class EmployeeDiscount(DiscountStrategy): # Extension: No existing code modified def apply(self, amount: float) -> float: return amount * 0.5 class Employee: def __init__(self, name, salary): self
class VIPDiscount(DiscountStrategy): def apply(self, amount: float) -> float: return amount * 0.8
class EmailSender(MessageSender): # Low-level def send(self, message: str) -> None: # SMTP logic here pass Deep Dive Issue: Python has no explicit interface keyword
from abc import ABC, abstractmethod class Bird(ABC): @abstractmethod def move(self): pass