PYTHON Contents

Classes and Objects (Mental Model)

Model domain concepts with classes, keep state explicit, and design objects that reduce coupling in production code.

On this page

Why Objects

Objects help you bundle data with behavior and define boundaries. In production, boundaries reduce accidental complexity and make failures easier to isolate.

Minimal Class

class RateLimiter:
    def __init__(self, limit: int) -> None:
        self.limit = limit
        self.count = 0

    def allow(self) -> bool:
        self.count += 1
        return self.count <= self.limit

Operational Checklist

  • Classes represent domain concepts, not random bags of functions.
  • State is explicit and initialized in __init__.
  • Public methods form a small, intentional API.

Failure Modes

  • God object: one class accumulates unrelated responsibilities.
  • Hidden state: behavior depends on implicit globals or module-level caches.