Python Intermediate Tutorial #2 - Inheritance
Skills:
AI Pair Programming80%
Key Takeaways
Python Intermediate Tutorial covering Inheritance and Operator Overloading using Python programming language
Full Transcript
what is going on guys welcome to the spite of the tourist series for intermediates episode number Tuesday will be about inheritance so again it is about object-oriented programming but this time we're getting a little bit deeper into the material so we're going to talk about inheritance what is inheritance basically imagine inheritance to be having a generalized class and classes that inherit from this class can be more specialized for example we could have a class animal which is pretty generalized and then we have the class dog the class cat the class parent for example and all these classes extend or inherit from the base class from the parent class which would be animal because all of these animals are animals obviously but not every animal is a dog not every animal is a cat but every dog is an element of animal sorry and every cat is an animal so we're going to create these class structures where we can have one general class and many different classes that inherit from this class so let us get into the code now let us just continue to work with our person class from the last video now person is a very generalized class like everyone is a person and if you want to narrow it down and put people into categories we could just go ahead and define a subclass that inherits from person for example something like worker not everyone is a worker so we have unemployed people and so on so it would be a categorization and what I could do in Python is of course I could just go ahead and define a new worker class completely independent from the person class so I just do everything again name age height and an additional value like celery and then I could just go ahead an or that I have the person class and start defining everything from the beginning self dot name equals name and so on but this is not reasonable because worker is just a person that has all the same elements attributes and all the same functions so it wouldn't be reasonable to do everything again but it would be reasonable to inherit from the person because it's actually the same thing but we're just extending the class so a worker still has a name an age and height so it's a person basically and to do that in pi think to say that a worker is just a person we use parentheses after the class definition and we we passed a parent class in this case a person which we want to inherit from so by saying that your worker and person in parentheses I say that the worker is a person and that I can treat it as a person so instead of assigning everything again I can use a special method called super super arm is accessing the parent class so what I can do here is I can just pass the class that I'm working with right now so worker and I also pass the object so self and then I can just access the init method so the constructor of my parent class so by doing this year I'm just accessing the init function that I already have up here so I'm not writing everything from scratch I have I can I can call the constructor function from the parent class and here I can again pass all the values age right and that's it and of course for salary I have to do it again because salary is not something every person has so self dot salary equals salary and of course I can just go add and define a new function here for example calculate yearly salary and we also have to pass the self here and then I just return the salary I have because it's monthly I return it times 12 months now I can go ahead and define a new worker work one equals worker and then I pass all the values here so let's say Henry Henry is 40 years old 176 centimeters high and has a salary of three thousand bucks a month so I can just print the information and of course also print the yearly salary so calculate yearly salary and you'll see that when I run this you'll notice one thing first of all sell go self-taught salary sorry self dot salary you'll notice one think that the print method here or the STR method the string method does not print a salary so in this case it prints the name the age and the height because we're still having a person here but it does not print our salary so if you want to change that we can do something which is called overriding methods that we already have so basically overriding and we do that by just overriding it's not a special thing where you just say def STR and pass self here and by defining the same function you just override the function that already exists so I can return actually I could just make something else here I can say text equals super and then call the super method here and call this string function so as TR of the parent class and of course then I can just add to this text the new text which would be salary and placeholder dot format self dot salary and then I can return the text so I think this should work as you can see the worse so we have all the values now because we've overwritten the method and we do this by just defining the same method again if it already exists we'll write it and that's basically how it works so here we have the worker which inherits from the person class now of course worker is still pretty generalized so we can even go further and say okay give me the class programmer which is a kind of worker because he works you have a salary but he maybe also has a favorite programming language and we can go much further with that so we can inherit and inherit again and inherit again so we can make big structures and trees of inheritance here if you want to even model the reality better now another thing I want to talk about in today's video is operator overloading and operator overloading is special because not every programming language allows you to do user-defined operator overloading for example Java does not allow it and Python does allow it I think C++ also allows it and you know it depends on a program a programming language but what operator overloading basically is is you can define yourself what happens when you add or apply any operator on two of your objects for example if we have two personal objects what shall happen when I add them to each other so person 1 plus person 2 what should happen or person 1 minus person 2 so we can define what happens when we apply operators ourselves Java does not allow us in Java you can now not to find that now in the example of Persons it might not make a lot of sense to add two persons to each other or to subtract them from each other because you can of course you can define you know I don't know add the two ages and choose the first name or something but usually it's used in other classes for example when we have vectors if you want to define vectors it makes kind of sense to define and at operation so what happens when you add two vectors to each other because if you don't know what vectors are they're just a vertical list in mathematics which tell you how much you want to go into the X direction how much you want to go into the Y direction and if it's three-dimensional also the Z direction but for two-dimensional vectors we just have the two values for x and y and we want to know what happens when we add two vectors when we add two vectors we add the two x our values in the two Y values and we get a new vector and this is what we want to model in Python right now and do that we have to create a class vector and this vector of course has a constructor self dot no X & Y so a vector has an X value and a y-value and we're going to assign that self dot x equals x and self dot y equals y and now we wanted to find what happens when we add one vector to another vector back to one two vector two and to do that we use the underscore underscore at underscore underscore method so like this again so in this case we define what happens when I add two vectors and as a parameter I get self of course and another thing so I don't I will call it other you can call it whatever you want but basically you have two parameters the first one is the vector itself that the object that you're applying the operation on and then you have another object that is also part of the operation so in this case what we could do is we could just return a new vector with self dot X plus other dot X and self dot y plus other dot Y and we can do the same thing with sup for subtraction let's just copy that here self-other and in this case of course we don't add but we subtract the two values now let's also define a string method here to print some information very simple just say return x placeholder and Y placeholder I'm typing like a right now dot format and then you say self dot X and self dot Y so this should be fine now we create a vector one which is a vector with the values I don't know two and five and then we have vector two which has the values six and three and now we can print the two vectors V 1 and print V 2 and then we can say K vector 3 equals vector 1 plus vector 2 and then we can print vector 3 so when we do that you will see that it works because we're adding 2 to 6 and 5 to 3 and the result is 8/8 so we're going eight into the x-direction and eight into the y-direction and of course i can do the same thing with subtraction here and I get a the difference so this is how operator overloading basically works so these are the basics of object-oriented programming in Python I hope you enjoyed it from the next video on we're no longer talking about object oriented programming in details so we're going to use it from time to time but we're not really going to talk about object-oriented programming concepts that much so these were the two first videos of this playlist and in the next video we're going into other topics so thank you very much for watching I hope you enjoyed the video hit the like button if you learned something ask questions give feedback in the comment section down below of course subscribe to this channel if you want to see more and thank you very much for watching see you in the next video bye [Music]
Original Description
In today's episode, we are talking about advanced object-oriented programming concepts like inheritance and operator overloading.
Website: https://www.neuralnine.com/
Instagram: https://www.instagram.com/neuralnine
Twitter: https://twitter.com/neuralnine
GitHub: https://github.com/NeuralNine
Programming Books: https://www.neuralnine.com/books/
Outro Music From: https://www.bensound.com/
Subscribe and Like for more free content!
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from NeuralNine · NeuralNine · 13 of 60
1
2
3
4
5
6
7
8
9
10
11
12
▶
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Visualizing Stock Data With Candlestick Charts in Python
NeuralNine
Python Beginner Tutorial #1 - Installation and First Program
NeuralNine
Python Beginner Tutorial #2 - Variables and Data Types
NeuralNine
Python Beginner Tutorial #3 - Operators and User Input
NeuralNine
Python Beginner Tutorial #4 - If Statements and Conditions
NeuralNine
Python Beginner Tutorial #5 - Loops
NeuralNine
Python Beginner Tutorial #6 - Sequences and Collections
NeuralNine
Python Beginner Tutorial #7 - Functions
NeuralNine
Python Beginner Tutorial #8 - Exception Handling
NeuralNine
Python Beginner Tutorial #9 - File Operations
NeuralNine
Python Beginner Tutorial #10 - String Functions
NeuralNine
Python Intermediate Tutorial #1 - Classes and Objects
NeuralNine
Python Intermediate Tutorial #2 - Inheritance
NeuralNine
Python Intermediate Tutorial #3 - Multithreading
NeuralNine
Python Intermediate Tutorial #4 - Synchronizing Threads
NeuralNine
Python Intermediate Tutorial #5 - Events and Daemon Threads
NeuralNine
Python Intermediate Tutorial #6 - Queues
NeuralNine
Python Intermediate Tutorial #7 - Sockets and Network Programming
NeuralNine
Python Intermediate Tutorial #8 - Database Programming
NeuralNine
Python Intermediate Tutorial #9 - Recursion
NeuralNine
Python Intermediate Tutorial #10 - XML Processing
NeuralNine
Python Intermediate Tutorial #11 - Logging
NeuralNine
Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
NeuralNine
Python Data Science Tutorial #2 - NumPy Arrays
NeuralNine
Python Data Science Tutorial #3 - Numpy Functions
NeuralNine
Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
NeuralNine
Python Data Science Tutorial #5 - Subplots and Multiple Windows
NeuralNine
Python Data Science Tutorial #6 - Matplotlib Styling
NeuralNine
Python Data Science Tutorial #7 - Bar Charts with Matplotlib
NeuralNine
Python Data Science Tutorial #8 - Pie Charts with Matplotlib
NeuralNine
Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
NeuralNine
Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
NeuralNine
Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
NeuralNine
Python Data Science Tutorial #12 - Pandas Series
NeuralNine
Python Data Science Tutorial #13 - Pandas Data Frames
NeuralNine
Python Data Science Tutorial #14 - Pandas Statistics
NeuralNine
Python Data Science Tutorial #15 - Pandas Sorting and Functions
NeuralNine
Python Data Science Tutorial #16 - Pandas Merging Data Frames
NeuralNine
Python Data Science Tutorial #17 - Pandas Queries
NeuralNine
Python Machine Learning Tutorial #1 - What is Machine Learning?
NeuralNine
Python Machine Learning Tutorial #2 - Linear Regression
NeuralNine
Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
NeuralNine
Python Machine Learning #4 - Support Vector Machines
NeuralNine
Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
NeuralNine
Python Machine Learning Tutorial #6 - K-Means Clustering
NeuralNine
Python Machine Learning Tutorial #7 - Neural Networks
NeuralNine
Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
NeuralNine
Generating Poetic Texts with Recurrent Neural Networks in Python
NeuralNine
Stock Portfolio Visualization with Matplotlib in Python
NeuralNine
Analyzing Coronavirus with Python (COVID-19)
NeuralNine
Making Text Images Readable Again with Python and OpenCV
NeuralNine
Neural Networks Simply Explained (Theory)
NeuralNine
Motion Filtering with OpenCV in Python
NeuralNine
Top 5 Programming Languages To Learn in 2020
NeuralNine
Simple TCP Chat Room in Python
NeuralNine
Image Classification with Neural Networks in Python
NeuralNine
Edge Detection with OpenCV in Python
NeuralNine
S&P 500 Web Scraping with Python
NeuralNine
Simple Sentiment Text Analysis in Python
NeuralNine
Introduction - Algorithms & Data Structures #1
NeuralNine
More on: AI Pair Programming
View skill →Related AI Lessons
⚡
⚡
⚡
⚡
X now offers an MCP server to make its platform easier for AI tools to use
TechCrunch AI
n8n Automation Repurpose Video Content: The 2025 Production Guide
Dev.to AI
You’re Still Paying $200/Month for AI Tools You Could Replace With a Free Local Setup Tonight
Medium · Data Science
Top 10 AI Tools Every College Student Should Know in 2026
Medium · AI
🎓
Tutor Explanation
DeepCamp AI