On 10/21/05, Andrew P <[EMAIL PROTECTED]> wrote: > 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 >
Hi Andrew, You've run into one of my biggest peeves when trying to learn OO. The whole "OO models real world objects" is detrimental when the examples that are given are pointless to programming. You'll never model someone ordering pizza. Alan Gauld's tutorial has a decent guide to OO, but it still takes a bit of getting used to. That's why I like Python, it doesn't force you into a certain style of programming, as opposed to something like, say, Java, and it's somewhat ludicrous Hello World - http://java.sun.com/docs/books/tutorial/getStarted/application/ The thing is, if you can use a list method or string method, you're using OO, and when you begin learning, you don't need to know too much about objects immediately. I attempted to learn programming via my local university, which taught Java, and our first lab had us creating the classes Tap (Faucet for the norteamericanos), Mixer and Basin. While, in retrospect, I can see that it included some essential bits of Java like multiple constructors, get/sets, subclassing etc, it was just so pointless, (as was the rest of the practical stuff until the very last assignment where one built a calculator) as all you were doing was designing classes that passed unit tests. Speaking for myself, I learn best when I come across a problem that needs a particular solution. I still don't know exactly what a composite is, but my best guess is that it's just a design pattern to simplify handling multiple instances, wherein multiple instances are passed in to one instance and controlled via methods of that object. I found this "Composite: this pattern is often used to handle complex composite recursive structures." which sounds about right. I can't think of a specific example for that. But I've just used something similar to it. In the iPod's database, data regarding a song comes in several different sections, each of which needs to be manipulated in it's own right. But, to make it easier, instead of having to handle seven/eight objects at once, I created a Song object, which has methods to act on those seven/eight objects for me. For instance, to change the artist of a song, you have to change an attribute of two objects, which the Song class does for me via one method. I hope that's a correctish example, hopefully someone will correct me if it's not. All the examples of composite objects I can find tend to be ahem.. rather abstract with many a leaf and node object. I do wish you luck in your endeavour. Regards, Liam Clarke _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor