From the course: Python Code Challenges for Object-Oriented Programming

Unlock this course with a free trial

Join today to access over 25,500 courses taught by industry experts.

Solution: Month delta

Solution: Month delta

- [Instructor] So let's head over to our test cases and look at what we have here. So you can see that the first test cases, we seem to have a new class called MonthOffset and we're adding MonthOffset to another MonthOffset. So we've got MonthOffset five added to MonthOffset three, and we should get a total of MonthOffset, month is equals eight. So if I just go ahead and select test my code, you can see that we've got a type error, unsupported operand type for MonthOffset and MonthOffset. And that's because we haven't created the code for that yet. So let's go ahead and update our class MonthOffset, with a dunder add, right, and so I'm going to create my method. So def dunder add. And I should be able to add a self and an other. And the first thing I want to check is make sure that the instance type is off type MonthOffset. So if isinstance, and other, MonthOffset. And then all I want to do now is just sum up the months, for self and other. So I'm going to return a MonthOffset object.…

Contents