Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll

- 2x 2x
- 1.75x 1.75x
- 1.5x 1.5x
- 1.25x 1.25x
- 1.1x 1.1x
- 1x 1x
- 0.75x 0.75x
- 0.5x 0.5x
Now that we can roll dice and compare them, it's time to start on the Yatzy game play.
Are you confused by how we're using the die_class
argument? Functions and classes are first-class citizens in Python which means we can pass them around and use them as values just like we would any other variable. That means we can point a variable or parameter at a class or function and then call that variable just like we would the original class. Here's another example:
class Car:
pass
class Van:
pass
class Motorcycle:
pass
def vehicle_factory(cls, count):
for _ in range(count):
yield cls()
Now we could make any number of Car
, Van
, or Motorcycle
instances that we want (and, notice, it's a generator function since we're using yield
; if our vehicle manufacturing required a lot of memory, we could still be polite to other processes and not tie up all of the RAM with our factory). We'd do this with code similar to
cars = vehicle_factory(Car, 50)
vans = vehicle_factory(Van, 10)
motorcycles = vehicle_factory(Motorcycle, 100)
Each time the for
loop executes, it finds the class that was passed in and creates an instance of it. This ability of Python makes for really flexible code.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up-
amandae
15,727 Points3 Answers
-
Eric Peppler
3,675 Points2 Answers
-
PLUS
Begana Choi
Courses Plus Student 13,126 Points1 Answer
-
jayda hendrickson
3,413 Points1 Answer
-
Jonathan Gonzalez
8,544 Points1 Answer
-
spencer tintorri
6,184 Points1 Answer
-
Adebayo Samuel Ojo
PHP Development Techdegree Student 12,790 Points1 Answer
-
Nicholas Abate
1,643 Points2 Answers
-
Kohei Ashida
4,882 PointsWhy is "__repr__" used here? and Why should it return with str() function?
Posted by Kohei AshidaKohei Ashida
4,882 Points3 Answers
-
Eldin Guzin
6,010 Points4 Answers
-
Eldin Guzin
6,010 Points1 Answer
-
Bharat B
535 Points2 Answers
-
Prateek Sharan Lall
1,778 Points2 Answers
-
PLUS
Amir Shahabnia
Courses Plus Student 29,310 Points1 Answer
-
jdee
6,655 Points4 Answers
-
PLUS
Jonathan Dell'Ova
Courses Plus Student 6,283 Points1 Answer
View all discussions for this video
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up