Introducing the super() Function
00:00 Sometimes you want to override a certain method, but maybe you just want to add a bit to the end or to the beginning. You don’t want to just override the whole thing, especially if the method is quite large.
super() function allows you to access the original method of the parent class while you’re still overriding it in your subclass. So to put that in a slightly different way, sometimes you need to extend a certain method, and within that method, you can call the parent class’s method with the
You’ve got your custom exception up here. You’ve got your
Point class, which has been abbreviated here, just so it all fits on the screen. You’ve got your
Shape class, which has a space here for something that we’re going to add in in a second.
01:22 But someone could pass in a list of numbers or strings, and as long as there are four of them, that would still work. So you really want to add in a check here to make sure each of them are points.
You might do this by saying
for point in points:
if not isinstance(point, Point), then you can raise a
TypeError. We’re not subclassing this because the
TypeError is quite a common type of error.
self.points = points, you’re calling
super().__init__(points). What’s happening here?
super() is like a reference to the parent, and then it’s saying, okay, get the parent, and then get the
.__init__() method from the parent here, and then we’ll pass in the points to that function.
You could put the
super() beforehand. That would also work. But it makes sense to have it after because we want to make sure that we’ve gone through all the checks before we assign the points to the instance as an attribute, namely that the code has checked whether the number of points is
4 and then whether all the points are actually instances of the
Point class, and finally assign the points to the instance.
Let’s look at that in action. With this code loaded up, you can see that the
Square shape has the
super() call in it, but if we were to revert this and put this back into
self.points, so it’s not using the
super() function, this is no longer paying attention to the check to make sure all the points are actually instances of the
You’ll see that it creates without any errors, and if you look at
box, you’ll see that it’s a
Square object, but if you look at the points, then you’ll see that this is just a list of numbers, not of points.
So we really want to catch this. The naive way might be to just copy and paste all of this into here. Now this won’t run. If I press Control + S and then F5 to run it, I’ll get an error. It’s saying all points should be members of the
Point class, so that works. But we’re duplicating code here, and we don’t need to.
So we can pass in the points. So we’re getting the points that were originally passed in, we’re checking to make sure there are four, and then we’re passing it to the original constructor here, and then that becomes the
(1, 0). Okay, so now we Control + S to save that and then F5 to run it, and we’re not getting any errors. And if we look at our
box, we’ll see that it’s a
Square object, and if we look at our points within that box, we can see that we have four
Point objects in there.
Become a Member to join the conversation.