Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Brandon Hoffman
Brandon Hoffman
3,642 Points

Unsure of what I am comparing in the doubles function (hands.py)

So the double property is supposed to compare two rolls and see if they get the same value and return true if so, but I am confused what I declare to get the values/instances of those two rolls within the double property. Can someone help point me in the right direction?

hands.py
from dice import D6


class Hand(list):
    def __init__(self, size=0, die_class=None, *args, **kwargs):
        if not die_class:
            raise ValueError("You must provide a die class")
        super().__init__()

        for _ in range(size):
            self.append(die_class())
        self.sort()

    def _by_value(self, value):
        dice = []
        for die in self:
            if die == value:
                dice.append(die)
        return dice


class CapitalismHand(Hand):
    def __init__(self):
        super().__init__(size = 2, die_class = D6)


    @property
    def ones(self):
        return self._by_value(1)

    @property
    def twos(self):
        return self._by_value(2)

    @property
    def threes(self):
        return self._by_value(3)

    @property
    def fours(self):
        return self._by_value(4)

    @property
    def fives(self):
        return self._by_value(5)

    @property
    def sixes(self):
        return self._by_value(6)

    @property
    def _sets(self):
        return {
            1: len(self.ones),
            2: len(self.twos),
            3: len(self.threes),
            4: len(self.fours),
            5: len(self.fives),
            6: len(self.sixes)
        }

    @property
    def doubles(self):


    @classmethod
    def reroll(self):
        ch = CapitalismHand()
        return ch 

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,457 Points

self is the list of current die values. Doubles would be if the first two items in the list were equal. The notation looks strange, but you can reference the die using self[0] and self[1].

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

Could you explain this further? I don't understand the principle behind that solution.

Chris Freeman
Chris Freeman
Treehouse Moderator 68,457 Points

Travel down the rabbit hole as deep as you wish....

The class CapitalismHand inherits from Hand which inherits from List so it's behavior is an extension of a regular list. Since the size attribute lead to creating a list of that length, doubles can be determined by looking at the first two elements in the list itself. The self variable is a pointer to this instance of CapitalismHand which is the list.

All attributes and methods of list are available to CapitalismHand, as is slicing and index notation. So self[0] and self[1] are the first two elements of the list. These elements are instances of the D6 class. This means that any attributes such as self[0].value refer to the D6 instance value, and any conversion calls such as int(self[0]) will run the D6.__int__ method.

Now to the comparison. The flow below may seem a bit convoluted.

A class must have (or inherit) an __eq__ method for comparisons to work. As seen the earlier Comparing and Combining Die video, The class D6 inherits from Die which gets both an __eq__ and an __int__ method added around 01:18 in the video.

    def __init__(self):
        return self.value

    def __eq__(self, other):
        return int(self) == other

Semantics notes: self is the CapitalismHand instance, self[0] and self[1] is are first two elements of the list where each is an instance of the D6 class, and other is whatever object is being compared to.

With these methods, two D6 instances can be compared, such as, self[0] == self[1] as follows:

  • self[0].__eq__ will be called with self[1] as other.
    • this method calls evaluates int(self) == other
    • this calls self[0].__int__() on "self" (the first D6 instance) which returns the integer self.value (the first D6 instance value)
    • say, self.value is an int 5, which leaves 5 == other ("other" in this case is the second D6 instance)
  • this causes self[1].__eq__ to be called with the second die as "self" and the "5" as other.
    • this method, again, calls evaluates int(self) == other
    • this calls self[1].__int__() on "self" (the second D6 instance) which returns the integer self.value (the second D6 instance value)
    • say, this self.value is an int 4, which now leaves 4 == 5, which fails, ultimately returning False