Python Object-Oriented Python Dice Roller Yatzy Scoring

Henry Lin
Henry Lin
11,636 Points

_by_value function?

In _by_value function, Kenneth did

for die in self:
    if die == value: 
        do something
   return dice

Does die == value work because he overrode __eq__ method in dice.py? Also return dice would return the actual number of each die's face is because he overrode __reper__ right?

[MOD: added formatting -cf]

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 61,991 Points

Does die == value work because he overrode __eq__ method in dice.py? Correct!

Also return dice would return the actual number of each die's face is because he overrode __repr__ right? In the _by_value method, "dice" is a list that will contain all of the die matching value append to the list.

In re-scanning the videos, I did not see where __repr__ was overridden.

Post back if you have more questions!

Johannes Scribante
Johannes Scribante
19,175 Points

Just to clarify, Hand() is a subclass of list and is not a subclass of Die(). In Die() the __repr__ was overridden, but even though Hand() contains a list of many Die() instances the __repr__ was not overridden in Hand().

We can however equate die == value because the list of Die() in Hand() each Die() instance is part of the Die class which has overridden the __eq__ magic method and therefore we can equate die == value?

Please correct me if I am wrong, I would just like to make sure I understand correctly