Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Object Inheritance in Python

Give Feedback

While Python isn’t purely an object-oriented language, it’s flexible enough and powerful enough to allow you to build your applications using the object-oriented paradigm. One of the ways in which Python achieves this is by supporting inheritance, which it does with super().

By the end of this course, you’ll be able to:

  • Compose a class
  • Use super() to access parent methods
  • Understand single and multiple inheritance

In this lesson, you’ll cover how to use super() to access a parent class’s constructor:

class Rectangle:
    def __init__(self, length, width):
        self.length = length
        self.width = width

    def area(self):
        return self.length * self.width

    def perimeter(self):
        return 2 * self.length + 2 * self.width

class Square(Rectangle):
    def __init__(self, length):
        super().__init__(length, length)

Download

Course Slides (PDF)

896.4 KB

Many thanks for the interesting course! May i ask what text editor did you use? :)

swapniltambe93 on May 4, 2020

Hi Christopher, thanks for this amazing video lecture. I just have one query which as follows, why are you using lenght twice here in class Square(Rectangle): def init(self, length): super().init(length, length)

Roy Telles on Aug. 11, 2020

I think the reason for passing length twice in super().__init__(length, length) is because a Square’s width is essentially its length. And since a Square is a Rectangle, we need to pass its length again to the Rectangle’s width parameter.

Christopher Trudeau RP Team on Aug. 12, 2020

Yes Roy, you’re right! The Rectangle class takes two parms its width and its length. The Square inherits from Rectangle and needs to pass in those same to parms. In the case of the Square the length and width are the same thing, so it gets passed in twice.

Christopher Trudeau RP Team on Aug. 12, 2020

I know this answer is very late, but in case anyone else is curious, I don’t use a text editor to do these demos. I find it hard to type and talk at the same time, so I wrote a custom utility that makes it look like I’m doing that. The tool uses Urwid, a curses-like command line GUI thing and Pygments for the colourization.

It is available on PyPI and the code is here:

github.com/cltrudeau/purdy

(the pedantic answer to your question is Vim though, I’m old and old-school :) )

Dan B on Nov. 13, 2020

The from shapes import Square bit was a surprise to me! Where can I learn about how to structure code in python?

Christopher Trudeau RP Team on Nov. 13, 2020

Hi Dan,

If you’re interested in packages and modules in Python, you might like the following course:

realpython.com/courses/python-modules-packages/

Happy coding!

Dan B on Nov. 13, 2020

Thanks.

Become a Member to join the conversation.