Vasu Patel logoVasu Patel
← Python Syntax & DSA Reference
All chapters in this track
  1. 011. Variables, Types & Operators
  2. 022. Strings
  3. 033. Lists
  4. 044. Tuples, Sets & Dicts
  5. 055. Control Flow
  6. 066. Error Handling
  7. 077. Functions & Lambdas
  8. 088. Comprehensions
  9. 099. Built-in Functions
  10. 1010. Type Hints (3.10+)
  11. 1111. Classes & Dataclasses
  12. 1212. Iterators & Generators
  13. 1313. DSA Standard Library
  14. 1414. Python 3.9–3.11 Features for DSA
  15. 1515. DSA Idioms & Gotchas
  16. 1616. Master Quick Reference

Chapter 11 of 16

11. Classes & Dataclasses

Tutorial

DSA primarily uses classes for node definitions. Know __lt__ for heapq compatibility and @dataclass for clean boilerplate-free classes.

# ══ DSA NODE CLASSES ═══════════════════════════════════════════
class ListNode:
    def __init__(self, val: int = 0, next: "ListNode | None" = None):
        self.val  = val
        self.next = next
    def __repr__(self) -> str:
        vals, cur = [], self
        while cur:
            vals.append(str(cur.val)); cur = cur.next
        return " → ".join(vals)

class TreeNode:
    def __init__(self, val: int = 0,
                 left:  "TreeNode | None" = None,
                 right: "TreeNode | None" = None):
        self.val   = val
        self.left  = left
        self.right = right

# Graph node (sometimes used)
class Node:
    def __init__(self, val: int, neighbors: list["Node"] | None = None):
        self.val       = val
        self.neighbors = neighbors if neighbors is not None else []

# ══ __lt__ FOR HEAPQ COMPATIBILITY ════════════════════════════
# heapq requires elements to be comparable
class State:
    def __init__(self, cost: int, node: int):
        self.cost = cost
        self.node = node
    def __lt__(self, other: "State") -> bool:
        return self.cost < other.cost   # heapq uses < operator

import heapq
heap = [State(5, "B"), State(1, "A")]
heapq.heapify(heap)
top = heapq.heappop(heap)   # State(1, "A")

# ══ DATACLASS ══════════════════════════════════════════════════
from dataclasses import dataclass, field

@dataclass
class Point:
    x: int
    y: int
    # auto-generates __init__, __repr__, __eq__

@dataclass(order=True)  # also generates __lt__, __le__, __gt__, __ge__
class Edge:
    weight: int          # compared first (field order matters)
    src: int = field(compare=False)  # excluded from comparison
    dst: int = field(compare=False)

# Push Edge directly into heap (order=True adds __lt__)
edges = [Edge(3, 0, 1), Edge(1, 0, 2)]
heapq.heapify(edges)

# @dataclass(frozen=True) — immutable, hashable (can be dict key / set element)
@dataclass(frozen=True)
class Coord:
    row: int
    col: int

visited: set[Coord] = set()
visited.add(Coord(0, 0))   # works because frozen=True makes it hashable

# ══ __slots__ (memory optimisation for many small objects) ════
class CompactNode:
    __slots__ = ("val", "next")   # prevents __dict__ creation
    def __init__(self, val: int):
        self.val  = val
        self.next = None

# ══ INHERITANCE (occasionally needed) ══════════════════════════
class Animal:
    def __init__(self, name: str):
        self.name = name
    def speak(self) -> str:
        return f"{self.name} makes a sound."

class Dog(Animal):
    def speak(self) -> str:             # override
        return f"{self.name} barks!"
    def fetch(self) -> str:
        return f"{self.name} fetches!"

d = Dog("Rex")
d.speak()    # "Rex barks!"
d.fetch()    # "Rex fetches!"
isinstance(d, Dog)     # True
isinstance(d, Animal)  # True — Dog is a subclass of Animal

Reference

DunderPurposeDSA use
__init__ConstructorNode initialisation
__repr__Debug stringVisualise linked list/tree
__lt__Less-than comparisonheapq compatibility
__eq__EqualityCounter, set membership
__hash__HashableSet/dict key (auto with frozen=True)
@dataclassAuto-generate __init__, __repr__, __eq__Cleaner node/edge classes
order=TrueAlso generate __lt__ etc.heapq without manual __lt__
frozen=TrueImmutable + hashableDict key, set element
field(compare=False)Exclude from orderingPriority + data in heap
__slots__No __dict__, less memoryLarge graphs
class Dog(Animal)InheritanceOverride methods; isinstance() checks subclass