Python OOP Tutorial (Object Orientated Programming ) - Inheritance

Tech With Tim · Beginner ·🛠️ AI Tools & Apps ·7y ago

Key Takeaways

This video tutorial by Tech With Tim covers Python Object Oriented Programming, specifically focusing on inheritance, allowing for the creation of new classes based on existing ones, and using tools like Python's class syntax and the super() function for method and attribute access. It demonstrates how to create efficient and readable code through inheritance.

Full Transcript

hey guys and welcome to the third video in my Python object and class tutorials series now in today's video I'm gonna be going over inheritance and this is an extremely important and a little bit more complicated topic dealing with inheriting attributes and methods from other objects I just want to start off by quickly saying though if you guys are finding the video is a bit too slow for you and I'm kind of explaining things a lot of time the reason I do that is because I'm not sure the level of person that's gonna be watching the video and I want to make sure that the people that will understand quickly can get the information within like the first few minutes and then the people that really need so extra reinforcement have that opportunity to watch through the entire like 11 or 12 minutes and have it explained to them multiple time so if you guys are finding it's a bit too slow and you want to move faster just fast forward the video or skip to the next one if you guys are done with whatever I've been talking about cuz like in the last video for example I watched it back and I kind of explained a few the same thing a few times that's just for the people that need a bit of extra help and that's kind of what I aim to do with a lot of my videos so that people that need help can you know grab that if they want to okay so let's get started and I'm just gonna modify this dog class just delete Li we'll get rid of change age and add weight and we're just gonna have name age so on like that okay and we can get rid of this stuff down here now what I'm actually gonna do is I'm gonna create a new class and I'm gonna call this cat okay now cat is gonna have very similar attributes as dog in fact let's just copy them over okay so we'll paste them like here we have name age hi I am whatever okay now I'm just gonna add one thing in this case I'm gonna add let's see color okay today self-taught color is equal to color okay now this cat object it's obviously gonna work the same as dog I don't think I need to show you now just take note of what I just did I just copied all of this stuff from here into cat now if you know anything about programming you know that programmers don't like to copy and paste and that you really shouldn't be copying and pasting code so python has actually come up with the solution to what I just did that allows us to not have to do that and that is called inheritance so I'm gonna delete all this and I'm gonna go back in to cat I'm gonna type dog you know what this little bracket up here means is it means parent so whenever you're talking about inheritance you always have a parent or a superclass and then you have a child or a like derived class okay and there's a bunch of different words for them they're not really that important just know parent and child it's the most basic okay so in this case cat is going to inherit from dog which means dog is the parent class and cat is the child class and it is derived so it's taken from dog so that means if I do class dog and if all I do in here is just type one thing so I type in its underscore underscore and in here I'm just gonna put name age and color and I'm just gonna do something that's gonna look confusing haven't explained it after okay super dots punch crunch coordinates underscore underscore underscore and I give it name age say self dot color equals color and then I create a new object of cat so I'm just gonna say tim equals cats give it a name agent colors in this case will do tim age five color bully blue cat I know okay so we do that we press play no errors okay I'm just gonna show you ready Tim dot speak what do you thinks gonna happen take a guess says hi I'm Tim and I'm five years old now how the heck does all this work and what did you just do I know that's probably the question you're asking probably fix that to say color so the way this works is when you inherit from another object or another class sorry you actually inherit all of the properties and attributes and methods of that class so when I do tim dot speak you can see well Tim is an object of type cat and cat well I don't see speak here how does that work well since the speak method belongs already in the dog class and we're inheriting from the dog class it is actually carried on to the cat class and we're able to use it like that okay so Tim that speak now what I did here this super dot an it I know this looks confusing but pretty much in the initialization of which is unique to cat because it has this added parameter color I simply called the initialization of dog first which means that when I gave it name and I gave it age it automatically added self dot name and self dot age to my cat object of Tim you see how here I didn't type self dot name equals name I didn't type self taught age equals age all I did was called the constructor method or the initialization method of the superclass and the superclass simply means dog and it did that for me now in this example it seems like well why in the world would you want to do that that like that doesn't make sense there's not that many lines whatever if you have really long methods and really long classes it makes a lot of sense to inherit so in this example it's hard to show but in long ones you do want to inherit now I want to show you that you can add and overload things from the parent class so for example the parent class has speak I'm gonna add one more method in here I'm gonna say define let's just say talk okay and in here it's gonna say print bark because it's a dog okay now obviously in our cut class if we want to talk we don't want to bark right and that's what's gonna happen right now if we do tim dot talk it's just gonna say park so if we wanted to change this we can actually overload this override this method so if I do define talking here I can print and in here now I'm like I'm not gonna do bark I'm gonna do yeah okay like that a little exclamation point so now if I do Tim dot talk this you can see that we get meow instead of bark and that goes for anything in inherited classes okay so anything that we do in here is going to override whatever is happening in this dog class so say we wanted to change speak we could say define speaking here and then whenever we put in this speak method would overwrite what's in here now that's not to say that it's gonna change if you create a dog dog objects of I do like I don't know let's say Jim is equal to dog give it a name give it Jim clearly age is an old dog meaning at 70 okay and we do Jim talk well we're still gonna get if I spell Jim correctly bark right so we get arc em yeah so we don't actually change anything in this class we just say that well if you create a class or if you could I can't object then it's gonna have this talk method which overrides the talk method that we created in dog and this is the same if you do something with like these attributes so if I do like self thought name and I just statically type in a name like I type tech okay then when we do Tim don't speak now you would think that that's gonna get hello I am Tim because that was the name right and I am five years old but instead we're gonna get tech so it says hi I am tech and I am five years old and that is because although we set name up here to be equal to whatever name we type in the pin as a argument we are changing it to tech and that overrides whatever happens in this dog class okay so I hope that makes sense now what else can we override from those classes well we can inherit like multiple times we can inherit different classes from upper class I'm going to talk about the way you'd want to do in inheritance okay so the way you typically want to inherit is you want to have one really general class that kind of applies that's a bit smaller that applies like a bunch of different classes so I'm gonna delete these and I'm just going to type out them and show you an example what I mean by general class so if I create a class and I called it vehicle like that okay it's not how you spell vehicle I don't care we're just gonna leave it like that okay and then I created an initialization here okay and the initialization is gonna take like price gasps and color okay I know these are random but just just fall along self-taught chrysippus price self thought gas just gas and self thought cut there equals color okay now I'm just gonna give it a method I'm gonna call it define fill-up tank like this okay and what students say in a self-taught gas equals I'm just gonna say that each gas tank has a maximum of 100 okay say define empty tank and this is just gonna say self dot gas equals zero and I'm gonna say define gas left and this is simply gonna return self dog gas okay and the reason I have this is because well in all vehicles we're probably gonna want to have gas right all of our vehicles are gonna have gas if they're not electric obviously we're not gonna deal with that for now so Philip tank empty tank and gas left is probably something we want to know for all of our different vehicles now for example if I create another class that's a class car okay and car is gonna inherit from vehicle like that okay and all we're gonna do for the Annette is we're just gonna call the vehicle in it so how we do that is to do super dot I just put a net underscore and obviously this needs now a price gas and a color so we're gonna pass it price gas and color like that and then that's our initialization and in car all we're gonna have is we're gonna have a method and it's gonna call what's mmm actually let's add one more thing here speed okay self dot speed equals speed okay and then here as a method let's just have like beep okay and all this is gonna do is simply print beep beep okay now I know these aren't great examples but just follow so we have now if you have the vehicle class okay and it has these things that deal with gas and obviously gas is gonna work for all of our different types of vehicles and then we create a car class that inherits your vehicle and in here all it does is it has an extra attribute called speed and it has a method called beep now affected another class instead of car I want to do truck now truck he's not gonna say beep beep it's gonna say honk honk like that ok and instead of having self that speed we're gonna have self thought tires ok equals tires because that's a property we're gonna care about for our trucks because sometimes eight it like six tires 18 you know what I mean that's driving my truck okay so you can see that we have a really general class vehicle that simply has a color a gas and a price and obviously within our truck and car class we're gonna care about those attributes of price gas color but in truck we care about tires and when we click beep we want to say honk honk and for a car we care about the speed that the car can go and when we call beep we want to sprint beep beep okay so that is an example of where you would use inheritance I know this is a really small example along the classes you're gonna end up building are gonna be like massive and inherit from like other classes and you're gonna have like large files full of classes but this is the best example I can kind of give you if one inheritance would be useful and same thing if I wanted to do like define motorcycle then you could do the same thing there and four wheels you do like self dot wheels equals to like stuff like that okay so I think I'm gonna end the video here hopefully you guys have an idea of how inheritance works one quick thing if I wanted to I could inherit from car as well so if something is inheriting from something you can also inherit from it so that would mean that now truck has all the attributes of car and all the attributes of vehicle because well car has the attributes of vehicle so since car has it we're inheriting from car truck is gonna get as well just want to add that in before yet anyways make sure you guys leave a like and subscribe and I will see you again in the next video [Music]

Original Description

In this Python Classes and Objects Tutorial we will be talking about python inheritance of classes. Inheritance is a very important and useful topic and can help us to make much more efficient and readable code. WEBSITE: https://techwithtim.net/tutorials/python-programming/classes-objects-in-python/inheritance/ ************************************************************** Support the Channel: https://www.patreon.com/techwithtim Twitter: https://twitter.com/TechWithTimm Join my discord server: https://discord.gg/pr2k55t ************************************************************** Please leave a LIKE and SUBSCRIBE for more content! Tags: - Tech With Tim - Python Tutorials - Classes in Python - Python Classes - Object and Classes tutorial python - Inheritance in Python
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Tech With Tim · Tech With Tim · 0 of 60

← Previous Next →
1 A* Path Finding Algorithm(Visualization)
A* Path Finding Algorithm(Visualization)
Tech With Tim
2 Python Programming Tutorial #1 - Variables and Data Types
Python Programming Tutorial #1 - Variables and Data Types
Tech With Tim
3 Python Programming Tutorial #2 - Basic Operators and Input
Python Programming Tutorial #2 - Basic Operators and Input
Tech With Tim
4 Python Programming Tutorial #3 - Conditions
Python Programming Tutorial #3 - Conditions
Tech With Tim
5 Python Programming Tutorial #4 - IF/ELIF/ELSE
Python Programming Tutorial #4 - IF/ELIF/ELSE
Tech With Tim
6 Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Tech With Tim
7 Python Programming Tutorial #6 - For Loops
Python Programming Tutorial #6 - For Loops
Tech With Tim
8 Python Programming Tutorial #7 - While Loops
Python Programming Tutorial #7 - While Loops
Tech With Tim
9 Python Programming Tutorial #8 - Lists and Tuples
Python Programming Tutorial #8 - Lists and Tuples
Tech With Tim
10 Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Tech With Tim
11 Python Programming Tutorial #10 - String Methods
Python Programming Tutorial #10 - String Methods
Tech With Tim
12 How to Overclock a NVIDIA GPU
How to Overclock a NVIDIA GPU
Tech With Tim
13 Python Programming Tutorial #11 - Slice Operator
Python Programming Tutorial #11 - Slice Operator
Tech With Tim
14 Python Programming Tutorial #12 - Functions
Python Programming Tutorial #12 - Functions
Tech With Tim
15 Python Programming Tutorial #13 - How to Read a Text File
Python Programming Tutorial #13 - How to Read a Text File
Tech With Tim
16 Python Programming Tutorial #14 - Writing to a Text File
Python Programming Tutorial #14 - Writing to a Text File
Tech With Tim
17 Python Programming Tutorial #15 - Using .count() and .find()
Python Programming Tutorial #15 - Using .count() and .find()
Tech With Tim
18 Python Programming Tutorial #16 - Introduction to Modular Programming
Python Programming Tutorial #16 - Introduction to Modular Programming
Tech With Tim
19 Python Programming Tutorial #17 - Optional Parameters
Python Programming Tutorial #17 - Optional Parameters
Tech With Tim
20 Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Tech With Tim
21 Python Programming Tutorial #19 - Global vs Local Variables
Python Programming Tutorial #19 - Global vs Local Variables
Tech With Tim
22 Python Programming Tutorial #20 - Classes and Objects
Python Programming Tutorial #20 - Classes and Objects
Tech With Tim
23 Cool VBS Script to Prank Your Friends!
Cool VBS Script to Prank Your Friends!
Tech With Tim
24 How to Overclock an AMD GPU
How to Overclock an AMD GPU
Tech With Tim
25 Best GPU'S For Mining Ethereum (2018)
Best GPU'S For Mining Ethereum (2018)
Tech With Tim
26 Recursion and Memoization Tutorial Python
Recursion and Memoization Tutorial Python
Tech With Tim
27 Ethereum Mining Rig - Hardware Guide
Ethereum Mining Rig - Hardware Guide
Tech With Tim
28 Pygame Tutorial #1 - Basic Movement and Key Presses
Pygame Tutorial #1 - Basic Movement and Key Presses
Tech With Tim
29 How to Install Pygame (Windows 8/10)
How to Install Pygame (Windows 8/10)
Tech With Tim
30 How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
Tech With Tim
31 How to Mine Ethereum 2018 - WORKING (Super-Easy)
How to Mine Ethereum 2018 - WORKING (Super-Easy)
Tech With Tim
32 Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Tech With Tim
33 Pygame Tutorial #2 - Jumping and Boundaries
Pygame Tutorial #2 - Jumping and Boundaries
Tech With Tim
34 Pygame Tutorial #3 - Character Animation & Sprites
Pygame Tutorial #3 - Character Animation & Sprites
Tech With Tim
35 Pygame Tutorial #4 - Optimization & OOP
Pygame Tutorial #4 - Optimization & OOP
Tech With Tim
36 OBS Studio Tutorial - Best OBS Settings
OBS Studio Tutorial - Best OBS Settings
Tech With Tim
37 Linear Search Algorithm - Python Example and Code
Linear Search Algorithm - Python Example and Code
Tech With Tim
38 Make Any Mic Sound AMAZING! (WITH OBS)
Make Any Mic Sound AMAZING! (WITH OBS)
Tech With Tim
39 Binary Search Algorithm - Python Example & Code
Binary Search Algorithm - Python Example & Code
Tech With Tim
40 Pygame Tutorial #5 - Projectiles
Pygame Tutorial #5 - Projectiles
Tech With Tim
41 Pygame Game - Mini Golf
Pygame Game - Mini Golf
Tech With Tim
42 Pygame Tutorial - Projectile Motion (Part 1)
Pygame Tutorial - Projectile Motion (Part 1)
Tech With Tim
43 Pygame Tutorial - Projectile Motion (Part 2)
Pygame Tutorial - Projectile Motion (Part 2)
Tech With Tim
44 Pygame Tutorial #6 - Enemies
Pygame Tutorial #6 - Enemies
Tech With Tim
45 Pygame Tutorial #7 - Collision and Hit Boxes
Pygame Tutorial #7 - Collision and Hit Boxes
Tech With Tim
46 Pygame Tutorial #8 - Scoring and Health Bars
Pygame Tutorial #8 - Scoring and Health Bars
Tech With Tim
47 Cloud Mining vs. Hardware Mining - 2018
Cloud Mining vs. Hardware Mining - 2018
Tech With Tim
48 How to Install Pygame on Mac OSX (Fast-Simple)
How to Install Pygame on Mac OSX (Fast-Simple)
Tech With Tim
49 Pygame Tutorial #9 - Sound Effects, Music & More Collision
Pygame Tutorial #9 - Sound Effects, Music & More Collision
Tech With Tim
50 Pygame Tutorial #10 - Finishing Touches & Next Steps
Pygame Tutorial #10 - Finishing Touches & Next Steps
Tech With Tim
51 How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
Tech With Tim
52 How to Create a Button in Pygame [CODE IN DESCRIPTION]
How to Create a Button in Pygame [CODE IN DESCRIPTION]
Tech With Tim
53 Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Tech With Tim
54 Pygame Side-Scroller Tutorial #2 - Random Object Generation
Pygame Side-Scroller Tutorial #2 - Random Object Generation
Tech With Tim
55 Pygame Side-Scroller Tutorial #3 - Collision
Pygame Side-Scroller Tutorial #3 - Collision
Tech With Tim
56 Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Tech With Tim
57 How to Create A Message Box in Python - Tkinter
How to Create A Message Box in Python - Tkinter
Tech With Tim
58 Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Tech With Tim
59 How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
Tech With Tim
60 Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Tech With Tim

This tutorial teaches Python Object Oriented Programming, focusing on inheritance to create new classes from existing ones, making code more efficient and readable. It covers using Python's class syntax and the super() function for accessing methods and attributes.

Key Takeaways
  1. Create a general class with attributes
  2. Inherit from the general class to create specific classes
  3. Use super() to call the parent class's initialization method
  4. Override methods and attributes in the child class
  5. Create objects from the child classes and access their attributes and methods
💡 Inheritance in Python allows for the creation of new classes based on existing ones, promoting code efficiency and readability by reducing duplication and facilitating the addition of new attributes and methods.

Related AI Lessons

Up next
How to Open HPL Files (HP-GL Plotter)
File Extension Geeks
Watch →