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
Dice are great but we need to be able to find ones that fit certain criteria, like being greater than four. It'd also be handy to be able to add dice together. We can do both of these things using magic methods!
If you want to get a lot of magic method goodies easily, check out attrs
. It's a solid library and makes a lot of common usages much easier.
Or, to stick with the standard library, check out the docs for functools.total_ordering
. You need to define __eq__
and then one of the other operations and Python will figure out the rest.
Before you ask
Yes, I could have done something like:
def __le__(self, other):
return int(self) <= other
Either format (long or short) is fine and produces the same result.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up-
Cameron Ganley
4,049 Points1 Answer
-
Tinotenda Mangarai
3,828 Points1 Answer
-
Tinotenda Mangarai
3,828 Points0 Answers
-
Nikolai Olekhnovitch
Front End Web Development Techdegree Graduate 25,617 PointsWe used __radd__ to add reflective additions to instances, why not now in the comparison methods? Thank you
1 Answer
-
Aizah Sadiq
2,435 Points2 Answers
-
Kortney Field
14,091 Points2 Answers
-
MOD
Jonathan Grieve
Treehouse Moderator 91,254 Points4 Answers
-
Radosław Kalina
13,085 Points1 Answer
-
ryanosten
PHP Development Techdegree Student 29,615 Points3 Answers
-
Akshaan Mazumdar
3,787 Points1 Answer
-
Saul Goldman
1,539 PointsIt tells me I get an unexpected key word argument "sides"? This on my super init line
Posted by Saul GoldmanSaul Goldman
1,539 Points1 Answer
-
Steven Tagawa
Python Development Techdegree Graduate 14,438 Points1 Answer
-
leonardo valdes
12,384 Points1 Answer
-
PLUS
Joseph Raker
Courses Plus Student 3,354 Points2 Answers
-
nuri jeon
14,376 PointsI don't know where to pass the arguments.(Between __init__ or super()__init__())
Posted by nuri jeonnuri jeon
14,376 Points1 Answer
-
Simon Amz
4,606 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