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 trialSimon Amz
4,606 Pointsdefine 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]
3 Answers
Chris Freeman
Treehouse Moderator 68,457 PointsGood 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
4,606 Pointsperfectly clear with different names!
thanks
chen liu
3,188 PointsIs 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
Treehouse Moderator 68,457 PointsThe 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.
Sebastian Popa
11,094 PointsSebastian Popa
11,094 Pointsvalue(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.