Scale up as you grow — whether you're running one virtual machine or ten thousand.

From GPU-powered inference and Kubernetes to managed databases and storage, get everything you need to build, scale, and deploy intelligent applications.

This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Amazing tutorial. Make me understand the inheritance in OOP, very clearly, for first time . Hats off to Lisa
I think there’s an issue in the section about super(). In the __init__() method for Trout, self is being passed to the parent __init__() method. But this argument is being used as the value for first_name. It’s not causing an issue because the next line in the example sets terry.first_name to “Terry”.
If you call super().__init__() from a child class, I think you need to include all the required arguments for the parent class’ __init__() method. I think the fix for this example is to add a parameter for first_name in the child class __init__() method, and pass that as an argument to super().__init__()?
Thank you for this tutorial, this is a really helpful resource!
Hello Lisa,
I came across your tutorial and I really like it, but I noticed some problems, for example like the one ehmatthes pointed out. I’ve taken the time to outline what the problems are, and how to correct them here: https://github.com/svetarosemond/digital-ocean-inheritance-correction
Take a look and let me know what you think.
Hi Lisa,
After being filed as spam for putting up some useful links, and then a second time for no apparent reason, I’m going to keep my message short(er):
Your tutorial was really helpful to me, but I do agree with ehmatthes. Overriding init doesn’t mean that the parent’s init changes-- it still maintains the same arguments. The child just has a different init that is called when a new child instance is created. When you call a parent’s init, you need to pass any non-‘self’ and non-default arguments as specified in the parent init’s definition; otherwise, an error occurs. The solution that ehmatthes proposed is the conventional/correct way to go about making sure the initialization goes smoothly.
That being said, I thought much of content was beneficial. The fun and intuitive examples were especially appreciated. Thanks for the help!
Best, V
Wow am I Impressed
I’m a programming beginner learning Python, trying to get clear on Classes and Objects. I’m also a life-long professional writer and former investment industry quant (an odd pairing to be sure).
This tutorial and the two that precede it are some of the clearest, most direct and lucid examples of expository instructional writing I’ve ever seen.
Absolutely amazing job! I’m old enough to have seen a lot and learned a lot. Nothing is complicated if it’s explained well by an expert instructor skilled at illuminating the inner simplicity that lies hidden underneath apparent outer complexity.
These tutorials achieve that and more. Hat’s off and bows, Lisa, in your general direction.
Thank you so much for this tutorial! I’m a beginner in Python and this made learning so fun and simple, great job!! :) I genuinely picked up more in this tutorial than in tons of others!
Could I also trouble you to explain a piece of code I found elsewhere in your own words? (I’ve used the shark example provided in the tutorial to replace the context of the code I found elsewhere so it might be easier)
Class Shark(Layer):
def __init__(self, **kwargs):
super(Shark, self).__init__(**kwargs)
I have read your other tutorial on the use of *args and **kwargs, but I still can’t seem to piece them together.
Perhaps my main confusion would be the use of Shark,self within the parentheses in super(Shark,self) and the use of **kwargs
Thank you so much and I appreciate your tutorial nonetheless! :)
Hi. Thanks for the tutorial, it was well written.
One part that doesn’t seem right, is where you create a Trout object terry = Trout() and then set the name: terry.first_name = “Terry”
Most programming designs frown on this as if anything happens between those 2 lines, you’ve created an indeterminate state, where a Trout may or may not have a name.
A better solution is to do this:
class Trout(Fish): def init(self, *args, water = “freshwater”): self.water = water super().init(*args) # Notice you don’t need to pass self into the parent init!
Now you can set the name when you create the object as before: terry = Trout(“Terry”)
Everything else is as before.
Hey Lisa,
I have some concerns over the super function, even if we pass in variables as parameters for super function init , it do not reset the child init param freshwater, as it works the following way when ever we call a variable on object, it first checks inside its class ie Trout and then if its not available it checks in parent class. Let me write code for better understanding
class Trout(Fish):
def __init__(self, first_name, water="freshwater"):
self.water = water
super().__init__(self, first_name)
I believe we can do the above way. Please let me know if i am doing anything wrong in my way of coding