Get startedGet started for free

Informal interfaces

Informal interfaces provide a set of methods that must be defined in all classes that implement that interface. Below is a class named Supplier that will act as an informal interface. In order for YogurtSupplier to fulfill the contract set out by the Supplier interface, it must define the methods take_order() and make_delivery(). In this exercise, you'll practice doing just that.

class Supplier:
  def take_order(self, product_name, quantity):
    pass

  def make_delivery(self, order_id, location):
    pass

This exercise is part of the course

Intermediate Object-Oriented Programming in Python

View Course

Exercise instructions

  • In the YogurtSupplier class, define the take_order() method to add an order to the self.orders dictionary.
  • Complete the contract set out by the Supplier interface by defining a make_delivery() method to print a message and delete the order stored using order_id from self.orders.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

class YogurtSupplier:
  def __init__(self):
    self.orders = {}
  
  # Finish defining the take_order() method
  ____:
    self.____[f"{product_name}_{quantity}"] = {
      "product_name": ____, "quantity": ____
    }
  
  # Implement a make_delivery() abstract method
  ____:
    print(f"Delivering order: {order_id} to {location}")
    del ____.____[____]
Edit and Run Code