Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Start a free Courses trial
to watch this video
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 upRelated 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