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: Negation and multiple months - Python Tutorial
From the course: Python Code Challenges for Object-Oriented Programming
Solution: Negation and multiple months
- [Instructor] In this final problem, we're going to be looking at negation and multiplication. And as you can see, the test cases, the first one's about negation and the other two are for multiplication. Now we're only going to be making our changes in the month offset class. And so with negation, let's go ahead and define our dunder method. So it's, let's define dunder neg. And it's pretty straightforward because all we're going to do is we're going to return our month offset and we're going to return the self.months, but we're going to change the sign. And so we're going to return that with a negative sign. And then for the multiplication, right, we're going to have our dunder mul. So with the self and other, now this time we're multiplying our month offset with an integer. And so we need to make sure we check if it's an instance of an integer. And if it is, we want to return the month offset object, and we're going to multiply the month. So that's self.months by other. Now, that's…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.