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 Object-Oriented Python Dice Roller Project Breakdown

Simon Amz
Simon Amz
4,606 Points

define attribute as value = value

Hi,

In the first video, you define the D6 class, by overriding the __init__ from the parent class.

And you wrote:

class D6(Die):
    def __init__(self, value = 0):
        super().__init__(sides = 6, value = value)

I don't understand the meaning to write value = value.

is it mean that the parameter 'value' from the Die class (as we are in the super().__init__() will take as a value, the 'value' of the parameter value from the D6 class?

[MOD: updated formatting -cf]

Sebastian Popa
Sebastian Popa
11,094 Points

value(keyword) = value(value) //or// value(the argument) = value(the actual value passed) //or// value(the attribute) = value(a variable)

Maybe this will help you understand that piece of code . Reply if this is not your problem and i understood wrong.

3 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,457 Points

Good question! There is a whole lot of value happening in the code. Let's break it down.

The Die class, takes two arguments: sides: the number of faces on the die, and value: an integer to be used for the value attribute. Both have default values, so neither argument is required to be passed in.

The D6 class takes one argument: value: an integer to be used for the value attribute. It also has a default

The 'value' used in D6.__init__ has a default value of 0. If value is specified during object instantiation, this will be passed along to the parent Die.__init__ through the super statement.

Renaming the value parameters and arguments below for easier explanation.

class Die:
    def __init__(self, sides=2, Dvalue=0):  
        if not sides >= 2:
            raise ValueError("Must have at least 2 sides")
        if not isinstance(sides, int):
            raise ValueError("Sides must be a whole number")
        self.value = Dvalue or random.randint(1, sides)

class D6(Die):
    def __init__(self, D6value = 0):
        super().__init__(sides = 6, Dvalue = D6value)

# Example: initialize a D6 instance
d6 = D6(D6value=3)  # assign a value 3 to the die

# The D6value is passed to the Dvalue parameter to be used by the parent __init__
# D6.__init__ calls super to complete initialize of Die
    super().__init__(sides=6, Dvalue=3)  # 3 shown here where D6value would be

# Die.__init__ used the value from super:
    self.value = Dvalue or ....  # self.value is assigned the value of Dvalue (3) passed in from the super call

It can be confusing when all the parameters and arguments are called "value" and when speaking of the "value of value", etc.

Post back if you have more questions. Good luck!

Simon Amz
Simon Amz
4,606 Points

perfectly clear with different names!

thanks

Is that 3 passed from the D6 class to the Die class and the passed back? It is kind of confusing and seems unnecessary.

Chris Freeman
Chris Freeman
Treehouse Moderator 68,457 Points

The value argument passed from D6 to Die effectively becomes the initial value. The attribute self.value is set to the value argument if it is non-zero. Otherwise, if the value is zero, then the generated random value is used as initial value.