I've been reading about composition vs inheritance, and went back to
"Learning Python" to look at something I found very confusing a year
ago.  Turns out I still find it very confusing :)

The code at the bottom was taken from the OOP chapter, it's a solution
to one of the end-of-chapter problems.

Honestly, this toy program is enough to make my head spin.  Am I too
stupid for OOP?  Here is me trying to decipher it:

 Starting here:

    x = Lunch(  )                       # Self-test code
    x.order('burritos')                 # If run, not imported

You have to look at the method of class Lunch:

    def order(self, foodName):      # Start a Customer order simulation.
            self.cust.placeOrder(foodName, self.empl)

check the instance name:

self.cust = Customer(  )

to see what class it belongs to, jump to class Customer, look up the
method being called:

 def placeOrder(self, foodName, employee):  # Place order with Employee.
        self.food = employee.takeOrder(foodName)

 which uses employee as an argument, which was, looking back up, an
instance of class Employee:

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

which seems to return an instance of food?  I don't really understand
that,  but I've already forgotten what it was I started off looking to
find :)

x.result(  )

Which is just entirely too much for my brain to hold at once.  I'm
sorry if this sounds like whining, but this entire program seems as
bad as trying to decipher the GOTO-crazy scribblings of a lunatic. 
There is no linearity, and the extra abstraction of the classes just
adds another layer of complexity.  Instead of functions and arguments
there are methods, classes, and instances being passed about
willy-nilly and no (for me) easy way to follow the path or make sense
of it.

Is this just an example of a technique with a constant complexity
factor that looks ridiculous on a toy program, but very quickly
becomes useful on larger ones?  Are there tools that make reading
something like this more clear?

Thanks,

Andrew


#############
# lunch program
#############

class Lunch:
    def __init__(self):            # Make/embed Customer and Employee.
        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 about its Food.
        self.cust.printFood(  )

class Customer:
    def __init__(self):                         # Initialize my food to None.
        self.food = None
    def placeOrder(self, foodName, employee):  # Place order with 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(  )                       # Self-test code
    x.order('burritos')                 # If run, not imported
    x.result(  )
    x.order('pizza')
    x.result(  )
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to