Python Object-Oriented Python Dice Roller Yatzy Scoring

nicole lumpkin
PLUS
nicole lumpkin
Courses Plus Student 5,328 Points

YatzyScoreSheet class like a middleman?

So the YatzyScoreSheet class isn't a list or some other object that holds items. It doesn't even have attributes. My questions are:

  1. Is it just a place to group together methods that take in (as arguments) and act on YatzyHand instances?
  2. Why wouldn't we want to implement all scoring methods in the YatzyHand class itself?

Thanks :)

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 61,988 Points

Great question! You are correct. YatzyScoreSheet inherits from the simple object class by default. It does not have at attributes. Additionally, all of the methods have a self parameter but it is never referenced. This implies these methods could have been decorated with @staticmethod and not include the "self" parameter.

Yes, YatzyScoreSheet is a basic place to group together all the scoring methods. Since every one of its methods expect to operate on a YatzyHand object, it would make just as much sense to include these methods inside of the YatzyHand class. One pitfall to the using YatzyScoreSheet to hold all the method is there is no guarantee that the hand passed in is an actual YatzyHand-class object.

One one argument for not putting all the YatzyScoreSheet methods into the Hand class is to keep YatzyHand class from being too cluttered.

Another way to code it is to create YatzyScoreSheet as a mixin. This involves not having a "hand" parameter in the methods and relying on the self to point at the YatzyHand object. Then the YatzyHand could be coded as:

class YayzyHand(YatzyScoreSheetMixin, Hand):

This effectively puts the methods "inside" of YatzyHand, but also keeps the scoring organized in a separate file.

Post back if you need more help. Good luck!!!

nicole lumpkin
nicole lumpkin
Courses Plus Student 5,328 Points

Wow Chris Freeman , creating YatzyScoreSheet as a mixin just blew my mind!!! I'll go play and I'll write back if I need help. :) Thanks again!!!

Anthony Albertorio
Anthony Albertorio
22,168 Points

Great Idea. Cleans up code nicely.