โšก Track 02 โ€” Mid-Level Masterclass

Advanced OOP, ABCs & Structural Protocols

Design extensible object architectures using Abstract Base Classes (ABCs), static duck-typing via `typing.Protocol`, custom attribute descriptors, and pythonic dunder protocols.

๐Ÿ‘๏ธ Views: 377 Page Views
โšก Mid-Level Python Interactive Studio

Interactive Sandbox Studio: 4 Tracks & 26 Competencies

๐Ÿ‘๏ธ 377 Total Page Views

Switch tracks and select any of the 26 core topics below to run production Python code, inspect CPython memory mechanics, and execute unit test assertion suites in real time.

๐Ÿ“Œ Select Topic (26 Total): Showing 8 Topics for Track 02
โšก Topic 13 of 26 โฑ๏ธ 5 min interactive
Module 13 of 26

13. Immutable Dataclasses & field() Config

dataclasses auto-generate __init__, __repr__, and comparison methods. Adding frozen=True enforces immutability for thread-safe value objects.

๐Ÿ’ก Core Engineering Principles

  • Boilerplate Reduction: Eliminates tedious __init__ assignment code.
  • Frozen Immutability: frozen=True raises FrozenInstanceError on modification.
  • field() Factories: Use default_factory=list for mutable container defaults.
middle_dataclasses.py
Ready (Python 3.13 Active)
๐Ÿ–ฅ๏ธ Execution Output & Test Results stdout
=== EXECUTION RESULT ===

Config Object: DatabaseConfig(host='localhost', port=5432, tags=['primary', 'postgres'])
โœ… Test Passed: Created frozen immutable dataclass with default factory list!
โšก 30-Second Developer Summary

Nominal vs Structural Subtyping in Python

  • ABCs (Nominal Inheritance): Inherit from `abc.ABC` and enforce mandatory interface implementation at instantiation time using `@abstractmethod`.
  • Protocols (Structural Duck-Typing): Inherit from `typing.Protocol` to define implicit compile-time contracts verified by static type checkers like Mypy without strict inheritance dependencies.

1. `@property`, `@classmethod`, and `@staticmethod`

`@property` encapsulates attribute access with getter/setter logic. `@classmethod` receives the class reference (`cls`) to implement alternative constructors. `@staticmethod` operates as an isolated utility bound to the class namespace.

class BankAccount:
    def __init__(self, owner: str, balance: float):
        self.owner = owner
        self._balance = max(0.0, balance)

    @property
    def balance(self) -> float:
        """Read-only property getter."""
        return self._balance

    @classmethod
    def create_empty(cls, owner: str):
        """Alternative constructor initializing empty account."""
        return cls(owner=owner, balance=0.0)

    @staticmethod
    def validate_amount(amount: float) -> bool:
        """Utility method with no access to self or cls."""
        return amount > 0.0

acc = BankAccount.create_empty("Alice")
print(acc.owner, acc.balance)

2. Abstract Base Classes (ABC) vs `typing.Protocol`

ABCs prevent direct instantiation of incomplete subclasses. Protocols enforce structural subtyping (compile-time interface contracts).

from abc import ABC, abstractmethod
from typing import Protocol

# 1. Nominal Interface via ABC
class StorageBackend(ABC):
    @abstractmethod
    def save(self, data: dict) -> bool:
        pass

# 2. Structural Contract via Protocol
class Renderable(Protocol):
    def render(self) -> str: ...

class DashboardWidget:
    def render(self) -> str:
        return "<div class='widget'>Widget Content</div>"

def display_component(item: Renderable):
    print("Render Output:", item.render())

display_component(DashboardWidget())

3. Attribute Descriptors (`__get__`, `__set__`, `__delete__`)

Descriptors customize attribute lookup, assignment, and deletion behavior across objects. They power Python ORMs (SQLAlchemy, Django ORM), validation fields, and `@property`.

class NonEmptyString:
    """Descriptor enforcing non-empty string validation."""
    def __set_name__(self, owner, name):
        self.private_name = "_" + name

    def __get__(self, instance, owner):
        if instance is None:
            return self
        return getattr(instance, self.private_name, "")

    def __set__(self, instance, value):
        if not isinstance(value, str) or not value.strip():
            raise ValueError("Value must be a non-empty string!")
        setattr(instance, self.private_name, value)

class Product:
    name = NonEmptyString()

p = Product()
p.name = "TeachCloud Pro"
print("Product Name:", p.name)

โญ Rate Track 02: Advanced OOP, ABCs & Protocols (1 to 5 Stars)

Your rating is stored directly in the database. Average: 5.0 / 5 (1 rating)

โ˜… โ˜… โ˜… โ˜… โ˜…
โ† Track 01: Advanced Fundamentals Next Track: Memory & Object Model โ†’