Python Object-Oriented Python Advanced Objects Double

Jake Williams
Jake Williams
2,434 Points

Multiplying the challenge

I know this is way off but I'm not sure how to get this to multiply.
class Double(int):
    def __new__(cls, *args, **kwargs):
        self = super().__new__(cls, *args, **kwargs)

    def __mul__(self, other):
        if int = 5
        if other = (int) *2
        return (int)* other

1 Answer

Chris Freeman
Chris Freeman
Treehouse Moderator 65,283 Points

Hey Jake Williams, you’ve created a really cool teaching moment!

In the early tasks, the __new__ method is missing a return statement. Something like return self works.

Now the cool part! For Task 3, you really only need to add “* 2” to the __new__ return statement.

This works because the self * 2 calls the __mul__ method inherited from int. However, since you created your own __mul__ method, it overrides the inherited version. Therefore your local method has to work to pass Task 3.

    def __mul__(self, other):
        return int(self) * other

would get it done ✅. Or, simply delete the whole __mul__ method.

Post back if you need more help. Good luck!!!