IniziaInizia gratis

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

Questo esercizio fa parte del corso

Intermediate Object-Oriented Programming in Python

Visualizza il corso

Istruzioni dell'esercizio

  • 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.

Esercizio pratico interattivo

Prova a risolvere questo esercizio completando il codice di esempio.

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 ____.____[____]
Modifica ed esegui il codice