[Python] Learn Python, exercise 6.7

Discussão em 'Python' iniciado por Stack, Março 4, 2025.

  1. Stack

    Stack Membro Participativo

    I'm reading Lutz & Ascher - Learn Python and I found this as a solution to one of the exercises:

    class Lunch:
    def __init__(self):
    self.cust = Customer()
    self.empl = Employee()
    def order(self, foodName):
    # start a Customer order simulation
    self.cust.placeOrder(foodName, self.empl)
    def result(self):
    # ask the Customer what kind of Food it has
    self.cust.printFood()

    class Customer:
    def __init__(self):
    # initialize my food to None
    self.food = None
    def placeOrder(self, foodName, employee):
    # place order with an Employee
    self.food = employee.takeOrder(foodName)
    def printFood(self):
    # print the name of my food
    print self.food.name

    class Employee:
    def takeOrder(self, foodName):
    # return a Food, with requested name
    return Food(foodName)

    class Food:
    def __init__(self, name):
    # store food name
    self.name = name

    if __name__ == '__main__':
    x = Lunch()
    x.order('burritos')
    x.result()
    x.order('pizza')
    x.result()`


    What I don't understand is how the definition of the method placeOrder inside the customer class works, more specifically, there is no class employee (just Employee) whose method placeOrder could be used.

    Continue reading...

Compartilhe esta Página