Python Object-Oriented Python Dice Roller Giving a Hand

Kafe Hezam
Kafe Hezam
10,990 Points

I don't understand what does die_class() in the for loop do and why is it usefull?

I just don't understand what die_class() do and what function is it calling?

2 Answers

Steven Parker
Steven Parker
182,603 Points

The "die_class" was provided as an argument to the "Hand", and defines what kind of objects (die) will be rolled. The argument passed in will be the name of the class that defines those objects.

Each time "die_class()" is called, an object of that class is created so that it can be added to the "Hand". So, for example, if a hand is created by calling *hands.Hand(size=5, die_class=dice.D6)" then you will get a set of 5 6-sided dice (which is typical for a Yahtzee game). Each call t "die_class()" in that case will actually be creating a "D6" object.

Chul Kim
Chul Kim
2,341 Points

Just wondering is die_class just a random name that holds the actual class?

And the () is always used to call a certain function or class?

Steven Parker
Steven Parker
182,603 Points

I wouldn't say "random", as the name "die_class" follows the "best practice" of being descriptive of what the variable is used for. And yes, you are completely correct in that it holds the actual class.

You are also correct that parentheses are required when you call any function or method. If arguments are used, they will be placed inside the parentheses, but the parentheses must still be there even if there are no arguments.