> > OOP is a step up from functions its true but you need to work to > > understand it or you will continue to get confused by trivial errors. > > Then appearently I don't work hard enough. > Im still getting confuse by this example. > > I wrote this piece : > > def print_hands(self): > for kaart in self.hands: > print kaart > return
When describing methods please tell us which class you are adding them to. Use the same trick your tutorial uses with ellipses: class ????: ... def print_hands(self): for kaart in self.hands: print kaart return In your case I assume you added it to the OldMaidGame class? > game = CardGame() Here you create an instance of CardGame... > hand = OldMaidHand("frank") > deck = Deck() > spel = OldMaidGame() Now you have an OldMaid hand a deck and an OldMaid game. You never use the deck? > game.deck.deal([hand], 13) Now you use the original CardGame instance to deal the OldMaidHand. > OldMaidGame.print_hands(spel) > > But now Im getting this error : > > AttributeError: OldMaidGame instance has no attribute 'hands' And that is true. When does the OldMaidGame create a hands attribute? Hint: It has no init method and the inherited CardGame init only creates a deck so it can't be when the instance is created. (This is why I said its always good to create an init method!) > def remove_all_matches(self): > count = 0 > for hand in self.hands: > count = count + hand.remove_matches() > return count > > So OldMaidGame has a attribute self.hands. Sometimes but not always. This means the class is "stateful" and is generally a bad design pattern. > I think Im giving up on OOP To be fair to you I don't think this example is a very good example of OOP. It is overly complex and disguising the points that its trying to teach with a quite convoluted set of interactions. This would be fine as a case study at the end of a series of topics on OOP, but to use this to teach inheritance is not helpful IMHO. The author starts off by saying: " The primary advantage of this feature is that you can add new methods to a class without modifying the existing class. ..." I don't think thats the primary advantage and in fact you are better if you do not add new methods but simply modify the existing ones! "....Also, inheritance can facilitate code reuse, since you can customize the behavior of parent classes without having to modify them. " This is the real power of inheritance, although not just for code reuse but to enable polymorphism. Which unfortunately the author does not even mention, let alone describe. "... inheritance can make programs difficult to read. When a method is invoked, it is sometimes not clear where to find its definition. The relevant code may be scattered among several modules. " The author appears to be attempting to illustrate this last feature of inheritance rather than the benefits! I suspect you may be better off finding a simpler introduction to OOP and coming back to this once you understand the basics more clearly. Alan G. http://www.alan-g.me.uk/ _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor