Python, properly · Chapter 23 of 42

Classes and objects

By the end of this chapter you can write a class, make objects from it, give them attributes in __init__ and behaviour in methods, and explain exactly what self is. 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 code so far keeps a rider as a dictionary and the things you do with a rider as loose functions. Bhavna found a rider whose deliveries never went up. The reason was one typo in a key:

rider = {"name": "Santosh Kamble", "area": "Andheri", "deliveries": 0}
rider["delivries"] = rider["deliveries"] + 1
print(rider)
{'name': 'Santosh Kamble', 'area': 'Andheri', 'deliveries': 0, 'delivries': 1}

A dictionary accepts any key, so the typo quietly made a new one. Nothing says which keys a rider must have, and nothing ties deliver() to riders: any function can be handed any dictionary. Kunal wants one place that says what a rider is and what a rider does. In Python that place is a class.

What you build: 4 tasks

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

  1. Task 1

    dish.py: a Dish class

    Write a class Dish. Dish(name, price) keeps both as the attributes name and price. Its method label() returns the name, a colon and the price, and price_for(qty) returns the price of qty of the dish.

    For Dish("Rajma Chawal", 170): label() returns "Rajma Chawal: Rs 170" and price_for(3) returns 510.

    The check also makes a second dish, and each one must keep its own name and price.

  2. Task 2

    order.py: an Order that collects dishes

    Write a class Order. Order(order_id, customer_id) keeps both as attributes, plus items: a dictionary from dish id to quantity, empty for a new order.

    add(dish_id, qty=1) adds qty of that dish (adding the same dish again adds to its count), and pieces() returns the total quantity.

    After add("roti", 4), add("chole") and add("roti", 2), items is {"roti": 6, "chole": 1} and pieces() returns 7. Every order must have its own items: adding to one never changes another.

  3. Task 3

    menu.py: a Menu read from menu.json

    Write a class Menu. Menu(path) reads the JSON file at path (a list of dishes, like /workspace/data/menu.json) once, when the object is made, and keeps the dishes on the object.

    price(dish_id) returns that dish's price, and available_names() returns a list of the names of the dishes whose available is true, sorted A to Z.

    For the real menu, price("misal-pav") is 120 and there are 22 available names. The check also makes a Menu from a small file of its own and deletes the file before calling the methods, so they must not read it again.

  4. Task 4 · interview question

    Interview: tiffin.py, fix the self mistakes

    tiffin.py has a Tiffin class with three mistakes, all about self. Tiffin(customer, capacity) holds at most capacity dishes: pack(dish) adds the dish to dishes and returns True, or returns False when the tiffin is already full, and is_full() says whether it is full.

    For Tiffin("Neel", 2), packing Chole, Jeera Rice and Roti returns True, True, False; dishes is then ["Chole", "Jeera Rice"] and is_full() returns True. Run the file and fix one traceback at a time.

What you will learn

  • A class is a blueprint; calling it makes an object (an instance).
  • __init__ gives each new object its own attributes.
  • Methods are functions on the class, and self is the object they were called on.
  • Attribute lookup: Python looks in the object first, then in its class.

More in Objects

Build "Classes and objects" 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.