Comparing and Combining Dice5:41 with Kenneth Love
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.
You need to sign up for Treehouse in order to download course files.Sign up