Python 3D Academyবাংলা

Designing with classes, dataclasses and Enums

🌍 In real life. Think of a food-delivery app. Each order holds data: the dish, the price, and where it is now. "Where it is now" is one of a few fixed steps: new, cooking, on the way, delivered. And the order moves forward one step at a time; it never jumps back.

That one sentence already tells you the design:

  • the fixed steps → an Enum, Status
  • the order's data → a dataclass, Order
  • moving forward → a method, advance(), so the rule lives in one place

The idea in one sentence: pick the tool by what the thing is: a fixed set of choices is an Enum, mostly data is a dataclass, rules to protect is a normal class, and "has a" is one object inside another.

Its words:

  • design: deciding which classes a program needs and how they fit together
  • state machine: an object that moves through fixed steps, like an order's status
  • composition: an object holding another object as an attribute

Step by step, designing the delivery order:

  1. The steps (new, cooking, on the way, delivered) are fixed choices → Status(Enum).
  2. The order is mostly data (dish, price, status) → @dataclass Order.
  3. "Move to the next step, never back" is a rule → a method advance() on the order.
from dataclasses import dataclass
from enum import Enum

class Status(Enum):
    NEW = 1
    COOKING = 2
    ON_THE_WAY = 3
    DELIVERED = 4

@dataclass
class Order:
    dish: str
    price: int
    status: Status = Status.NEW

    def advance(self):
        if self.status != Status.DELIVERED:
            self.status = Status(self.status.value + 1)

o = Order("biryani", 250)
o.advance()
o.advance()
print(o)
print(o.status.name)
Status(self.status.value + 1) finds the next step by its value. The if stops at DELIVERED.

Which tool for which job? Ask what the thing is:

  • a fixed set of choices (sizes, days, steps, colours) → Enum
  • mostly data, with a few small methods (an order, a book, a point) → @dataclass
  • rules to protect or lots of behaviour (a bank account that refuses a negative balance) → a normal class, with properties and methods
  • one thing that has another (an order has a status, a car has an engine) → keep it as a field: composition

Good design is mostly picking the simplest tool that fits, and putting each rule in exactly one place.

from enum import Enum

class Coin(Enum):
    ONE = 1
    TWO = 2
    FIVE = 5

def total(coins):
    return sum(c.value for c in coins)

purse = [Coin.FIVE, Coin.TWO, Coin.TWO, Coin.ONE]
print(total(purse), "taka")

Why design first? A few minutes deciding "what is each thing?" saves hours later: the right tool gives you checks (Enum), free methods (dataclass) and safe changes (methods) without extra code, and the next reader understands the program from its classes alone.

Mistakes and edge cases.

  • Plain strings for fixed choices. "on teh way" slips through; an Enum member can't be misspelt.
  • A dataclass for something with rules. If a field must never be negative, add a check or use a normal class with a setter.
  • Changing state from outside. order.status = Status.NEW after delivery breaks the "never back" rule; change it only through advance().
  • Inheriting for has-a. An order has a status; it isn't one.

Quiz

  1. A pizza can be SMALL, MEDIUM or LARGE. What fits best?

    • An Enum
    • A dataclass
    • Three separate classes
  2. A Contact holds a name, a phone number and an email. What fits best?

    • A dataclass
    • An Enum
    • A list of three strings
  3. What does this print?

    from enum import Enum
    
    class Step(Enum):
        NEW = 1
        DONE = 2
    
    class Task:
        def __init__(self, title):
            self.title = title
            self.step = Step.NEW
    
        def finish(self):
            self.step = Step.DONE
    
    t = Task("homework")
    t.finish()
    print(t.title, t.step.name)
  4. What does this print?

    from dataclasses import dataclass
    from enum import Enum
    
    class Size(Enum):
        S = 1
        L = 2
    
    @dataclass
    class Shirt:
        colour: str
        size: Size = Size.S
    
    print(Shirt("blue"))
  5. Which line has a mistake?

    from enum import Enum
    
    class Status(Enum):
        NEW = 1
        DONE = 2
    
    status = Status.NEW
    if status == "NEW":
        print("Just started")

Coding tasks

  1. Status is written for you. Write a dataclass Order with dish (str), price (int) and status (default Status.NEW), and a method advance() that moves to the next status but stays at DELIVERED.

  2. Write an Enum Coin with ONE = 1, TWO = 2, FIVE = 5, and a function total(coins) that adds up the values of a list of coins.

  3. Write an Enum Priority with LOW = 1, MEDIUM = 2, HIGH = 3, a dataclass Ticket with title (str) and priority (a Priority), and a function most_urgent(tickets) that returns the title of the ticket with the highest priority.