Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Object-Oriented Python Dice Roller Comparing and Combining Dice

Steven Tagawa
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Steven Tagawa
Python Development Techdegree Graduate 14,438 Points

Inheriting magic methods

This might be a dumb question, but...

If the D6 class inherits the comparison operator magic methods from the Die class, doesn't that mean that if you wanted to, you could just collect all those magic methods in a separate class by themselves, and then use that class as a parent class for any class that you need them for? Like:

class MathOperators:
    def __eq__(self, other):
        return self == other
# etc.

Then--

class MyMathClass(MathOperators):

So you wouldn't have to re-enter them every time you write a new program...?

1 Answer

Steven Parker
Steven Parker
231,248 Points

The "Die" class is intended to be a parent to others, such as "D6". So essentially, this implementation already does as you are suggesting.

But, yes, you could also extract those operations into another layer if you wanted a more generic "math" parent.