CommencerCommencer gratuitement

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

Cet exercice fait partie du cours

Intermediate Object-Oriented Programming in Python

Afficher le cours

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.

Exercice interactif pratique

Essayez cet exercice en complétant cet exemple de 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 ____.____[____]
Modifier et exécuter le code