From the course: Getting Started with Python Object Oriented Programming: A Hands-On Approach
Unlock the full course today
Join today to access over 24,800 courses taught by industry experts.
Solution: Class inheritance - Python Tutorial
From the course: Getting Started with Python Object Oriented Programming: A Hands-On Approach
Solution: Class inheritance
(upbeat music) - [Instructor] How did you get on? Let's look at what a solution may look like. So we have class Dog with a capital D, and we need to inherit from Animal. Then we have the constructor. Now for the constructor, we have self as always, or at least something equivalent to self. Technically it doesn't have to be self, but you need to be consistent, but let's just leave it as self for now. And then we need the name and we need the sound, because these are needed by the parent class. Then we're going to call super, which is the parent class itself, the init method of the parent class. Now, in the last video, we talked about what arguments we actually need to pass it. It's just going to be name and sound. We don't need to pass self along. Then the actual new method itself, so def wag_tail. This does require self and only self, and we're going to print a message using format strings or a format string, and we're going to do self.name, and then apostrophe s tail is wagging…