Python, properly · Chapter 26 of 42

Inheritance vs composition

By the end of this chapter you can build a class on top of another one, override its methods and reach the original with super(), read a class's MRO, and decide when an object should have another object instead of being one. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Objects
  • 4 tasks
  • Python
  • In the all labs plan

The first 2 chapters of Python, properly are free. This one comes with the all labs plan.

Why you need this

DabbaDrop's riders ride bikes, cycles and e-bikes. Bhavna wants delivery times per rider, and the only thing that differs between them is the speed. Copying the whole Rider class three times would mean fixing every bug three times. Meanwhile Rafiq wants every order to carry a bill, and someone has proposed class Order(Bill), which would make an order a kind of bill, with every bill method on every order.

Both are questions about how classes relate. Python gives you two answers: inheritance ("a cycle rider is a rider") and composition ("an order has a bill"). Choosing well keeps the code small; choosing badly glues unrelated things together.

What you build: 4 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    dishes.py: a combo is a dish

    Write a class Dish: Dish(name, price) keeps name and price, and label() returns "Roti: Rs 15" for Dish("Roti", 15).

    Then ComboDish(name, price, parts), a subclass of Dish that also keeps parts (a list of dish names). Its label() is Dish's label followed by the parts in brackets, joined by a comma and a space: ComboDish("Veg Thali", 210, ["Dal Tadka", "Roti", "Jeera Rice"]).label() returns "Veg Thali: Rs 210 (Dal Tadka, Roti, Jeera Rice)".

    Dish stores name and price for the combo too, and the combo's label is built on Dish's: the check changes Dish's init and label and expects the combo to follow.

  2. Task 2

    riders.py: riders that differ only in speed

    Write a class Rider: Rider(rider_id, name) keeps both, has a class attribute speed_kmph = 20, and minutes_for(km) returns the minutes to ride km at that speed, rounded up to a whole minute. Then BikeRider (30), CycleRider (12) and EBikeRider (25), subclasses that change only speed_kmph, with no methods of their own.

    Rider("R0", "Test").minutes_for(5) is 15, BikeRider 10, CycleRider 25, EBikeRider 12.

    Last, make_rider(data) takes one rider dictionary from /workspace/data/riders.json and returns a BikeRider for vehicle "bike", a CycleRider for "cycle" and an EBikeRider for "e-bike", with its rider_id and name.

  3. Task 3

    order.py: an order has a bill

    Write a class Bill: Bill() starts with lines, an empty list; add(name, price, qty=1) appends the tuple (name, price, qty) to lines; total() returns the sum of price times qty.

    Then a class Order that does not inherit from Bill: Order(order_id, customer_id) keeps both and a new Bill of its own in the attribute bill; add(name, price, qty=1) adds the line to its bill; total() returns its bill's total.

    After add("Chole", 150) and add("Roti", 15, 4), the order's bill.lines is [("Chole", 150, 1), ("Roti", 15, 4)] and total() returns 210.

  4. Task 4 · interview question

    Interview: staff.py, super() in a diamond

    Write four classes with a method duties() that returns a list. Staff returns ["sign in"]. Packer(Staff) and Driver(Staff) return what super() gives plus "pack" and "drive". PackingRider(Packer, Driver) returns what super() gives plus "hand over".

    Packer().duties() is ["sign in", "pack"], Driver().duties() is ["sign in", "drive"], and PackingRider().duties() is ["sign in", "drive", "pack", "hand over"]. The check also mixes your classes with classes of its own, so every list must come from super().

What you will learn

  • Inheritance: a subclass gets every attribute and method of its parent.
  • Overriding a method, and super() to call the parent's version.
  • The MRO: the exact order Python searches classes, even with two parents.
  • Composition: keeping another object in an attribute and handing it the work.

More in Objects

Build "Inheritance vs composition" yourself

DabbaDrop, a Mumbai tiffin startup, runs on Excel and WhatsApp. You are its first Python developer. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Python, properly are free. This one comes with the all labs plan.